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

Does list allow duplicates in java?

683


How do you include a string in java?

710


What are runtime exceptions?

865


Which one will take more memory: an int or integer?

944


What is the tradeoff between using an unordered array versus an ordered array?

889






What is constructor and virtual function? Can we call a virtual function in a constructor?

865


What if I write static public void instead of public static void in java?

797


What are different types of arrays?

710


What are the major drawbacks of external iteration?

775


Can you explain inner class.

804


What are abstract classes and anonymous classes?

813


What is ph and buffers?

705


Which list does not allow duplicates in java?

696


Why is java architectural neutral?

814


Can we overload run() method in java?

810