File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Virtual Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Virtual Constructor" Watch "Virtual Constructor" New topic
Author

Virtual Constructor

sm mishra
Greenhorn

Joined: Dec 12, 2006
Posts: 5
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
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
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
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
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
Ooops... my mistake
just ran a simular test myself...
thanks for the info.
Monty
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Virtual Constructor