Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance question

 
Jeff Wachhorst
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having a mental block here- Say Door extends Room, Room extends House. If Jeff extends House, can Jeff call one of Door's methods? If so, how?
 
Darin Niard
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you draw it out, it will make sense.

House -> Room -> Door
House -> Jeff

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 ]
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,

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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic