Can an abstract class have a constructor?

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(); // 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Enlist few advantages of inheritance?

592


Can an interface extend another interface?

604


What are user defined exceptions?

596


What are locale settings?

558


Is string is a data type?

575






What is arrays sort in java?

581


What is substring in java?

625


Explain the overview of UDP messaging.

743


Explain about anonymous inner classes in java?

586


What is the relationship between class and object?

531


How to display all the prime numbers between 1 and 100

512


Is class is a data type?

582


What is multithreading in java?

563


Define iterator and methods in iterator?

551


Explain inner classes ?

657