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

Tell me about different OOPS concepts.

593


Explain access specifiers?

661


What differences exist between iterator and listiterator?

573


What is anagram of a string?

519


What are implicit objects in java?

553






What are the four versions of java?

547


What is functional interface in java example?

537


Why bytecode is called bytecode?

590


What is the base class of all exception classes?

586


What is methods in java?

516


What do you mean by singleton class in java?

516


What are basic data types?

563


What is the largest long allowed by java?

538


What is string literal in java?

555


Is boolean a data type in java?

523