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 |
Can subclass overriding method declare an exception if parent class method doesn't throw an exception?
How do you input a string in java?
which of tha following is not a thread safe class? a) ArrayList b)Vector c)HashTable d)None
How the elements are organized in GridLayout?
how to create multithreaded program? Explain different ways of using thread? : Java thread
What is meant by javabeans?
What is Restrictions in hibernate?
why java does not support mulitple inheritance directly?
What causes memory leak in java?
What is AppletStub?
Why scanner is used in java?
Convert Binary tree to linked list.