Originally posted by John Hembree:
One of the choices is The relationship between Cat and Dog is an example of an appropriate use of inheritance.
This is a tricky point. Syntatically, this
is proper use of inheritence. In fact, there is a definite benefit from using inheritence in this case. Had you not had Cat inherit from Dog, you'd have to redefine the four legs, the fur, and the implementations of the eat and sleep methods within the Cat class. So, in terms of syntax, this is not only a valid use of inheritence, it's a good use of inheritence.
However, if we think of this situtation logically, we come to a totally different conclusion. When we say that something inherits from something else, we say that the inheriting object "is a" inherited object. So, in this case, we'd conclude that a cat "is a" dog.
Obviously, we know better.
If you look at the properties contained by a Dog, there is nothing there that really pertains
only to a dog. There are lots of things that have 4 legs, fur, and shed (a cat being one of them). In this case, it might have made more sense to put those attributes into thet pet class and allow Dog and Cat to both extend that class, like this:
This is probably a better use of inheritence because, not only does it make sense syntatically, but it also makes sense logically. A Dog "is a" Pet and a Cat "is a" Pet but a Dog is not a Cat and vice versa.
Of course, this is not the only acceptable way to do such a thing. Obviously, not all pets have four legs (what about fish). It is quite possible that you could have a Pet class taht has some great generalizations (like eating and sleeping) and have subclasses of pet that are slightly more specific, like this:
This inheritence hierarchy might even make more sense depending upon your context. In this case, not all pets have four legs and fur, but they all eat and sleep.
Hopefully, this helps you out.
Corey