Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Contruct a sub class instance from a super class instance.

 
Padma Priya
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 111
8
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic