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

Define how can we find the actual size of an object on the heap?

0 Answers  


How can we achieve thread safety in java?

0 Answers  


What is advantage of java?

0 Answers  


Why unicode is important?

0 Answers  


"we cannot create an object of interface but we can create a variable of it".diacuss the statement with the help of an example.

2 Answers  


Why do we create public static method in java?

0 Answers  


How is java hashmap implemented?

0 Answers  


What is return used for in java?

0 Answers  


What is the maximum length of a url?

0 Answers  


What is locale?

0 Answers  


how we can create packages in java?

0 Answers  


What is reverse function?

0 Answers  


Categories