File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan's Exam Q Doubt. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dan Watch "Dan New topic
Author

Dan's Exam Q Doubt.

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
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.

Answer given is ----
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 ??
Thanks a bunch in advance.
[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ January 30, 2004: Message edited by: Jessica Sant ]

"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Jessica Sant
Sheriff

Joined: Oct 17, 2001
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 ]

- Jess
Blog:KnitClimbJava | Twitter: jsant | Ravelry: wingedsheep
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
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

Joined: Oct 17, 2001
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

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Vad Fogel:
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.


Vad,
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.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
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

Joined: Sep 08, 2003
Posts: 116
Thanks Jessica, Vad and Dan.
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
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Good luck, Vishy! It's about time to hear some good news from you...
Philippe Maquet
Bartender

Joined: Jun 02, 2003
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 ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Dan's Exam Q Doubt.
 
Similar Threads
Dan's Mock Comp. Exam 15, Question 24
Dan's question - clarify!!!
Inheritance- cat dog / pets
Inheritance question
Mock exam question regarding inheritance plz explain this output