Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Dan's Exam Q Doubt.

Vishy Karl
Ranch Hand
Posts: 116
Hi Friends,
Following is the Q of Dan's exam.

Question 16

Which of the following statements is not a true statement?
a. A Cat object inherits an instance of Fur and four instances of Leg from the Dog superclass.
b. A Cat object is able to sleep and eat.
c. A Cat object is able to climb a tree.
d. The relationship between Dog and Pet is an example of an appropriate use of inheritance.
e. The relationship between Cat and Dog is an example of an appropriate use of inheritance.
f. None of the above.

e
The relationship between Cat and Dog is an example of an appropriate use of inheritance.
An appropriate inheritance relationship includes a subclass that "is-a" special kind of the superclass. The relationship between the Dog subclass and the Pet superclass is an example of an appropriate inheritance relationship, because a Dog "is-a" Pet. The relationship between the Cat subclass and the Dog superclass is not an example of an appropriate use of inheritance, because a Cat is not a Dog.
I don't understand why "because a Cat is not a Dog.". I think as Cat extends Dog so it Cat is a Dog. Hence e should not be the correct answer. How is e correct instead of d ??
[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ January 30, 2004: Message edited by: Jessica Sant ]

Jessica Sant
Sheriff
Posts: 4313
Originally posted by Vishy Karl:

I don't understand why "because a Cat is not a Dog.". I think as Cat extends Dog so it Cat is a Dog. Hence e should not be the correct answer. How is e correct instead of d ??

So, when the question askes you about the validity of these statements:
d. The relationship between Dog and Pet is an example of an appropriate use of inheritance.
e. The relationship between Cat and Dog is an example of an appropriate use of inheritance.

It's asking you if they make sense -- if the relationship that they created is a good example of the "is-a" or "has-a" relationship.
So -- in the realworld. "A Dog is a pet" is a totally valid statement. In your design, it would be good if you could mirror this. As we have shown above there is "class Dog extends Pet" -- so yes, Dog is-a Pet.
BUT then we get to the next subclass... "class Cat extends Dog". Back in the real world we know that interprets to saying "Cat is-a Dog." Well that's just flat out wrong. A cat is not a dog -- so that is not a good example of inheritance. And should not be used that way.
The point is to make sure when you're creating classes and subclasses that your inheritance structure makes sense.
So -- How could you create an inheritance structure for Pet/Dog/Cat that would work (make an appropriate usage of inheritance) without duplicating too many members (methods and variables)? What if you created a new class called Mammal, would that help?
[ January 30, 2004: Message edited by: Jessica Sant ]

Ranch Hand
Posts: 504
I see where Vishy's coming from. What makes answer e correct is only a matter of pure semantics. Change Dog to be SuperGadget and Cat to be SubGadget (you're free to come up with more meaningless names), and you receive option f. Technically speaking, I don't see anything wrong with a Cat object being a Dog (you never know these days :roll: ). The real exam will probably not unleash your philosophical ability to judge rather obfuscated concepts like that.

Jessica Sant
Sheriff
Posts: 4313
I understand waht he's saying (and yourself) -- and yes if you rename the classes to subgadget and supergadget -- then "subgadge is-a supergadget" -- but that's not the question asked.
" The relationship between Cat and Dog is an example of an appropriate use of inheritance.
Cause the exam might check to see if not only you understand the semantics of inheritance, but also its proper and appropriate usage.

Dan Chisholm
Ranch Hand
Posts: 1865
I see where Vishy's coming from. What makes answer e correct is only a matter of pure semantics. Change Dog to be SuperGadget and Cat to be SubGadget (you're free to come up with more meaningless names), and you receive option f. Technically speaking, I don't see anything wrong with a Cat object being a Dog (you never know these days :roll: ). The real exam will probably not unleash your philosophical ability to judge rather obfuscated concepts like that.

You are correct. If a subgadget really is a special kind of supergadget, then it is appropriate to use inheritance to model the relationship. If a subgadget is not in reality a special kind of supergadget, then it would not be appropriate to model the relationship using inheritance.
Here's an example of what can happen when inheritance is allowed to create inappropriate relationships. Suppose that you add the competeInCanineSports method to the dog class. If Cat is a subclass of Dog, then every instance of Cat also implements the competeInCanineSports method. Obviously, the cats won't perform well in canine sports, so Cat should not be modeled as a special kind of Dog.

Ranch Hand
Posts: 504
Dan,
Your point is crystal clear to me and, hopefully, to the most of aspirants. No doubt, the question is valid. Imagine, however, a guy, who knows nothing but Java (zoology is out of scope ). Gotcha, your question got him, 'cause from the Java's stand point, there's nothing wrong with how inheritance is implemented there.

Vishy Karl
Ranch Hand
Posts: 116
I was thinking from the syntax point of view and not thinking in the sense that Jessica explained. (modelling the real world)
So I guess the answer in the exam is correct.
Thanks again;
(Vad thanks for being always there to solve my queries) Hopefully I can give you all some good news on Monday -- scheduling my exam on monday)
Ok then ,byee

Ranch Hand
Posts: 504
Good luck, Vishy! It's about time to hear some good news from you...

Philippe Maquet
Bartender
Posts: 1872
Hi Vishy,
I found the question a bit confusing too.
1. I examined option E as you did (from a pure technical point of view), so I excluded E.
2. And I chose D because Dog doesn't inherit anything from Pet. Pet is a pure abstract class (no fields / no method implemented), so extending Pet is quite comparable to implementing a Pet interface, which could be better BTW by offering a more flexible design. Hence my confusion.
Regards,
Phil.
[ January 31, 2004: Message edited by: Philippe Maquet ]