| Author |
abstract classes, constructors
|
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
hi, i have the following question that i don't get... an abstact class can have a constructor, but how that constructor is called if we can't instantiate the abstract class !!?
|
- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. <br />- What truth? <br />- That there is no spoon!!!
|
 |
Vicky Nag
Ranch Hand
Joined: Jul 02, 2003
Posts: 40
|
|
Abstract classes can never be instantiated. But if you create a subclass of an abstract class and implement the abstract functions, you can then create an instance of the subclass. When you create an instance of the subclass, if the abstract class has a constructor that will be called as a part of initialsation process.. Hope this helps. V abstract class Base { Base() { System.out.println("Base"); } public abstract void Test(); } class Child extends Base { Child() { System.out.println("Child"); } public void Test() { System.out.println("Child Test"); } public static void main(String args[]) { new Child(); } } Output will be: Base Child
|
 |
Alton Hernandez
Ranch Hand
Joined: May 30, 2003
Posts: 443
|
|
|
|
 |
Vicky Nag
Ranch Hand
Joined: Jul 02, 2003
Posts: 40
|
|
Abstract classes can never be instantiated. But if you create a subclass of an abstract class and implement the abstract functions, you can then create an instance of the subclass. When you create an instance of the subclass, if the abstract class has a constructor that will be called as a part of initialsation process.. Hope this helps. V abstract class Base { Base() { System.out.println("Base"); } public abstract void Test(); } class Child extends Base { Child() { System.out.println("Child"); } public void Test() { System.out.println("Child Test"); } public static void main(String args[]) { new Child(); } } Output will be: Base Child
|
 |
Vicken Karaoghlanian
Ranch Hand
Joined: Jul 21, 2003
Posts: 522
|
|
|
thanks guys
|
 |
 |
|
|
subject: abstract classes, constructors
|
|
|