Can an abstract class have a constructor?



Can an abstract class have a constructor?..

Answer / 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

More Core Java Interview Questions

how to make a un-checked exception as a checked exception one.

2 Answers  


How to perform merge sort in java?

0 Answers  


Can we override the static methods?

0 Answers  


If a class is declared without any access modifiers, where may the class be accessed in java programming?

0 Answers  


how to deploy tomcatserver to weblogic server? write d following steps?

0 Answers  






Is java based on c?

0 Answers  


Why call by value prevents parameter value change?

0 Answers  


How does list work in java?

0 Answers  


2) Suppose there are 5 directories having lot of files (say txt files) in each directory. 2 things :- 2.1) You want to search for filenames which have a particular pattern. 2.2) Out of these filtered files you want to search for a particular keyword or a search string. How can you achieve this?

0 Answers   RBS, TCS,


Can a class have more than one object?

0 Answers  


Can we create constructor in abstract class ?

0 Answers  


How do you achieve singleton?

0 Answers  


Categories