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

How are Observer and Observable used?

3 Answers  


what is deadlock? : Java thread

0 Answers  


There are two classes named classa and classb. Both classes are in the same package. Can a private member of classa can be accessed by an object of classb?

0 Answers  


Explain some best practices you would apply while using collection in java?

0 Answers  


is it possible to add a object in a HASHMAP

6 Answers   HCL,






What is sizeof in java?

0 Answers  


What is a class ?

8 Answers  


Can we override final method?

0 Answers  


What are basic keywords?

0 Answers  


Which variable is the independent variable?

0 Answers  


What is the concept of multithreading?

0 Answers  


How is a variable stored in memory?

0 Answers  


Categories