• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dan's Exam Q Doubt.

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good luck, Vishy! It's about time to hear some good news from you...
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
There were millions of the little blood suckers. But thanks to this tiny ad, I wasn't bitten once.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic