• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instantiate subclass based on superclass instance  RSS feed

 
Jim Coble
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following class structure:


I have a Person object and I want to create a Participant object based on it (i.e., use case is that Person is now a Participant). The only way I've been able to figure out to do this is to add a constructor to the Participant class that takes a Person object as a parameter, like this ...

I can't help but thinking there must be a better way to do this. What am I missing?

Thanks.
--Jim
[ August 16, 2006: Message edited by: Jim Coble ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you make your reference to the object Person, but make the object a Participant object?
 
Jim Coble
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith, thanks for your response.

However, if I'm correctly following what you're suggesting, the reason I don't create the object as a Participant object to begin with is that I don't know that I will want it to be a Participant at the time I create it ... at that point, all I know is that it is a Person. The reason that I don't create it as a Participant just in case is that I have other subclasses of Person (e.g., User). Besides, not every Person is a Participant, so it would seem sort of non-OO to create a Person as a Participant just in case I later decided that Person is a Participant.

If the way I described in my original post is the best (or only) way to handle this, that's fine. Just wanted to be sure I wasn't missing something obvious.

--Jim
 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jim -

Your suggested approach makes sense to me. In effect, you are changing a reference's associated object type from Person to Participant. The new Participant object is an entirely new entity that simply uses values from the Person object to supply initialization values. I am curious to hear other opinions.

If I understand you correctly, the code would look something like this:

[ August 16, 2006: Message edited by: Bridget Kennedy ]
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Coble:
I can't help but thinking there must be a better way to do this. What am I missing?
I don't think you're missing anything. You need a Participant object, and you don't have one. So you have to create one. Therefore you need to use a Participant constructor. And the use case is that this constructor needs a Person object to work with. Hence, what you came up with. It doesn't have to be more complicated than that.

By the way there IS a name for this, if it makes you feel better that lots of other people did it before you: "Copy Constructor".
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the vague feeling that you might be better of with composition here.

What if instead of thinking of Users and Participants "being" Persons, you think of them as being roles that are attached to a person?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D'oh! Ilja beat me to it once again. The temptation to morph an object instance from one thing into another is usually a sign that things can be structured better.

A common homework problem is to model a school because students are very familiar with the real world structure, and a common problem is a grad student who teaches a class or an instructor who audits the class of another instructor. You can't morph a Person into GradStudent and Instructor at the same time. And it would really confuse other objects that happen to have a reference to the Person at the time.

So in that case and in yours, look at how a Person and a Participant behave differently. If they have the same methods but do different things, maybe Person should delegate method calls to a DefaultBehavior and a ParticipantBehavior. If Participant has more methods, it might implement the Person interface and the Participant interface and delegate the Person methods to a Person object.

If all this sounds interesting or raises more qeustions than it answers, stroll or scroll down to the OO, UML, etc forum and ask away.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!