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 is a parameter example?

736


What is main difference between variable and constant?

792


When a thread is executing a synchronized method , then is it possible for the same thread to access other synchronized methods of an object ?

833


Define jit compiler?

858


What is the this keyword?

793


When throws keyword is used?

770


What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?

889


What is the difference between this() and super() in java?

786


What 4 doubled?

748


Tell us something about set interface.

781


What is purpose of keyword void?

815


What is super keyword explain with example?

785


Can we change the value of static variable?

721


What is a dynamic array java?

731


Which class is the superclass for every class in java programming?

788