aspose file tools*
The moose likes Beginning Java and the fly likes Contruct a sub class instance from a super class instance. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Contruct a sub class instance from a super class instance." Watch "Contruct a sub class instance from a super class instance." New topic
Author

Contruct a sub class instance from a super class instance.

Padma Priya
Ranch Hand

Joined: Feb 01, 2007
Posts: 113
Hi All,

I have an instance of a super class. I would like to construct a sub class instance from this super class instance. Can you all tell me how we can do it. And can we use a factory method to do this.

Any help appreciated.

Thank you in advance!!

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Just create one. For example if the name of the subclass is Potato and it has a zero-argument constructor then use this code:

You could call the method containing that code a "factory" method if you liked.

However I'm guessing there is something more to the question which you haven't mentioned, since that seems like a very obvious answer.
Jason Bullers
Greenhorn

Joined: Dec 27, 2011
Posts: 28

Yeah, more information would be good. Creating an instance of a subclass from the super class sounds very odd to me, and it couples your classes. The situation Paul Clapham mentioned (a factory method) is the only obvious reason for it, to my mind. Maybe there is a different approach for what you are trying to do that's a little cleaner?
 
wood burning stoves
 
subject: Contruct a sub class instance from a super class instance.