The only thing Jeff and Door have in common is that they are both Houses, so the answer is no. However, Jeff will have all of House's methods, so in a sense yes, "Jeff can call some of Door's methods" but that isn't a very good way to put it. [ July 24, 2004: Message edited by: Darin Niard ]
Another important concept to consider is the fact that inheritance is an "is-a" relationship. For example, a Cat is-a Animal so it is appropriate for Cat to extend Animal. The animal superclass might contain some methods such as eat(), drink(), sleep() and many others that are appropriate for Cat and other subclasses of Animal. The Cat class might contain new methods such as climbTree(), catchBird() and many others that could be unique to a Cat.
In terms of the example posted earlier, a Door is not a Room, so it is not appropriate for a Door to extend Room. Instead, it is more appropriate to say that a Room "has-a" Door, so it would be appropriate for a Room class to have members of type Door.
It is not appropriate for Door to extend Room, because a Room might have things that we wouldn't expect to find in a Door. The Room class might contain methods such as setFloorType() and getFloorType() that would not be appropriate for a subclass of type Door, because a Door is not a Room. For that reason, a Door class should not be a subclass of Room.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Originally posted by Dan Chisholm: Another important concept to consider is the fact that inheritance is an "is-a" relationship.
Caution: This is a very slippery slope. "is-a" and "has-a" aren't very well defined and should merely be used as heuristics to decide between inheritance and delegation.
Another problem is that Java actually binds implementation inheritance (whichs main use is the reuse of code) with interface inheritance (whichs main use is the definition of subtypes). Therefore in Java a subclass preferably should also be a subtype.
That is, according to Liskov's Substitution Principle, a subclass should be substitutable for its superclass. With other words, a client of a class should without problems also work with all of its subclasses.
I agree, though, that letting Room, Door and Jeff inherit from House looks rather inappropriate...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Joined: Jul 02, 2002
Yes, I agree that Liskov's Substitution Principle is more clearly defined than the term "is-a". I always think of the "is-a" relationship as something that should support Liskov's Substitution Principle.