Can an abstract class have a constructor?



Can an abstract class have a constructor?..

Answer / 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(); // onstructor
call
ta.showAbstract();
ta.show();

}

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

}

}

Is This Answer Correct ?    70 Yes 2 No

Post New Answer

More Core Java Interview Questions

heavy components means what?

4 Answers  


What are the features of junit?

0 Answers  


What are memory tables?

0 Answers   Aspiring Minds,


What is void in java?

0 Answers  


What is tostring () method?

0 Answers  






Explain the importance of import keyword in java?

0 Answers  


What is object-oriented programming?

0 Answers  


What is the benefit of inner / nested classes ?

0 Answers  


What is the implementation of destroy method in java. Is it native or java code?

0 Answers  


What is nan in java?

0 Answers  


What is a lambda expression ? What's its use ?

0 Answers  


What are meta-annotations?

0 Answers   Cyient,


Categories