Hi! What is Virtual constructor in C++? What is the use of it? Does this Exsists in java.
Jakob Bosshard
Greenhorn
Joined: May 30, 2000
Posts: 21
posted
0
Hi There is no such thing as a virtual constructor in Java. Since constructors are related to a particular instance the following rules apply: Constructors can't be abstract, native, static, synchronized or final. However, Java does not prevent you from defining a non-abstract constructor in an abstract class. Note that you cannot instantiate an abstract class but you can use it as follows in a class hierarchy: The following code illustrates the assigment of a non-abstract subclass to an abstract superclass. So you can't have abstract constructors directly but in this sense it works fine. public abstract class AbstractTest { int i; public AbstractTest(int i) { this.i = i; } public static void main(String [] args) { AbstractTest test = new AbstractTestChild(1); } } class AbstractTestChild extends AbstractTest { public AbstractTestChild(int i) { super(i); //works } } Hope that this helps Regards Jakob
sm mishra
Greenhorn
Joined: Dec 12, 2006
Posts: 5
posted
0
Hi! I Know There is No Virtual Concept in Java. I want to know what is Virtual Constructor in OOPS(C++)
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
FYI Your statement: Constructors can't be abstract, native, static, synchronized or final. Correction to your statement: Construstors can be final ... Please review the Math class Monty [This message has been edited by monty6 (edited June 08, 2000).]
Jakob Bosshard
Greenhorn
Joined: May 30, 2000
Posts: 21
posted
0
Hi Monty Regarding the Math class: The class is final but the constructor is private, that is why you cannot find any constructor in the API Doc. Constructors can definitely not be final. Any book will tell you so or just simply try the following code: public final class FinalConstructor{ //o.k. final FinalConstructor() {} //illegal } This will give you a compiler error that states what constructors cannot be: final, native etc. Do you agree? Regards Jakob
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Ooops... my mistake just ran a simular test myself... thanks for the info. Monty