The Artima Developer Community
Sponsored Link

On Long Term is-a Relationships



This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


On Long Term is-a Relationships

Posted by Bill Venners on 25 Oct 1998, 7:01 PM

> I was intrigued by your statement that you may not necessarily model Employee as a subclass of Person, which is something I have always thought to be the case. How would you then pass an Employee to a method that expects a Person?

> I suspect that Employee would have a contructor that takes a Person as an argument and would also have a method getPerson() which returns a reference to that person object. This solution seems a but ugly to me since you would really like to pass that Employee around the system as if it were a Person.

Actually, in some situations, class Employee could be a
permanent is-a relationship to class Person. It all
depends on the particular problem you are trying to solve.

I think the point I want to make is that sometimes is-a
relationships can be impermanent, and in such cases inheritance
probably isn't the right design choice.

The Employee/Person example I chose arbitrarily
because I could imagine that being
employed might be a temporary activity of a Person. In other
words, a Person could be an Employee part of the time. But,
if you are just modelling people in one company, and if a
person leaves the company you just throw away the object, then
Employee could be seen as a permanent specialization of Person
and inheritance would be a good way to model that. On the other
hand, if every Person in your program is by definition an
Employee, then why have two classes at all?

I think the key question to ask is: In your situation, is the
is-a relationship you want to model with inheritance going to
be permanent throughout the lifetime of your program and,
to the extent you can see the future, the lifecycle of the
product? If so, I would use inheritance.



Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us