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
Is a method a procedure?
What is reverse function?
Explain runtime exceptions?
What is import java util arraylist?
What is skeleton and stub?
What is a variable simple definition?
Define a package.
Explain the importance of finally block in java?
write a program that list all permutations of ABCDEF in which A appears before B?
who can i handle multiple client in RMI
What is the difference between arraylist and hashset in java?
How to reverse string in java?
What are the 6 boolean operators?
What classes of exceptions may be thrown by a throw statement?
What is difference between path and classpath variables?