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 an i/o filter in java programming?

592


What does sprintf mean?

519


How can we achieve thread safety in java?

669


What are the restriction imposed on a static method or a static block of code?

580


What are the Static and Dynamic Variables? Differentiate them.

588






Do you need to import math in java?

551


Can a method be static?

513


What are the important methods of java exception class?

558


Why string is a class?

534


Are primitives objects?

550


What is output buffer?

556


what is the significance of listiterator in java?

605


Can Exception handling we can handle multiple catch blocks?

635


What are the different types of java?

541


What is java util concurrentmodificationexception?

481