• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Virtual Constructor

 
sm mishra
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
What is Virtual constructor in C++?
What is the use of it?
Does this Exsists in java.
 
Jakob Bosshard
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! I Know There is No Virtual Concept in Java.
I want to know what is Virtual Constructor in OOPS(C++)
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooops... my mistake
just ran a simular test myself...
thanks for the info.
Monty
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic