Can you have a constructor in abstract class?

Answer Posted / jag bhushan

yes,
we can have constructor in abstract class.
But we can not make instance of the abstract class.
instead we can make a reference to that abstract class.
and when we make a new object of the class which extends
the abstract class, the constructor of abstract class get
called.

see the code for example:

public abstract class TestAbstract {

TestAbstract(){

System.out.println("...in abstract class'
constructor");
}

public abstract void showAbstract();
public void show(){
System.out.println("...in show");
}
}


public class Test extends TestAbstract{
public static void main(String[] args) {

TestAbstract ta = new Test(); //
constructor call
ta.showAbstract();
ta.show();

}

public void showAbstract() {
System.out.println("...in showAbstract");

}

}

Is This Answer Correct ?    22 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is super in java?

769


What is linked hashmap and its features?

775


What are constructors in java?

762


What is a return in java?

786


Which method you will use to create a new file to store some log data. Each time a new log entry is necessary, write string to the file in java ?

912


What is threaded programming and when is it used? : Java thread

755


Can we serialize arraylist in java?

768


What is anti pattern in programming?

724


What is difference between path and classpath variables?

803


What is the difference between final, finally and finalize() in java?

783


What are the main features of java?

736


What is object of class in java?

826


Explain covariant method overriding in java.

780


How do you bind variables?

769


Why we use set in java?

762