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

What is treeset in java?

541


What invokes a thread's run() method in java programming?

565


Can we override static methods in java?

581


What does system out println () do?

553


Which methods are used during serialization and deserialization process?

550






What is sortedmap interface?

550


Do you need to import math in java?

557


Can we override the static method?

576


Define immutable object?

577


What is a class in java?

578


How many boolean functions are there?

502


What technique can be employed to compare two strings?

569


What is java string pool?

539


How to use Media tracker Class.

642


What is the difference between pass by reference and pass by pointer?

491