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 Inheritance- cat dog /  pets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance- cat dog /  pets" Watch "Inheritance- cat dog /  pets" New topic
Author

Inheritance- cat dog / pets

Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
class Leg{}
class Fur{}
abstract class Pet {
public abstract void eat();
public abstract void sleep();
}
class Dog extends Pet {
Leg leftFront = new Leg(), rightFront = new Leg();
Leg leftRear = new Leg(), rightRear = new Leg();
Fur fur = new Fur();
public Fur shed() {return fur;}
public void eat() {}
public void sleep() {}
}
class Cat extends Dog {
public void ignoreOwner() {}
public void climbTree() {}
}

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.

The answer is given e and explainetion is as follows:

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 special kind of a Dog. The goal of the OO paradigm is to develop software models that are accurate and reusable. If the software model is not accurate, then it probably is not reusable and the goals of the OO paradigm are not achieved. Code reuse and maintenance becomes increasingly difficult when inheritance is used to model inappropriate relationships. For example, suppose that somebody implements a herdSheep method in the Dog class. The Cat subclass would inherit the method and suddenly each instance of Cat would acquire the unwanted capability to make an attempt to herd sheep. It is difficult to imagine that a Cat would perform well in that role, so additional maintenance would be required to resolve the problem.

i think the xplaination and answer do not conform to each other the answer shud be D according to the above theory.Please clarify!


SCJP 1.4 (90%)<br />SCWCD 1.4 (88%)
No�l Verdurmen
Ranch Hand

Joined: Jul 28, 2004
Posts: 33
Technically the inheritance is correct. But it is very confusing to call a cat some specific kind of dog. (IS-A relation)

That is why it is inappropriate use. I think the explanation you copied is correct.


No�l
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Swati Udas:
...
I think the xplaination and answer do not conform to each other the answer shud be D according to the above theory.Please clarify!


Swati,

Thank you for using my exam.

The question on my mock exam is as follows.

"Which of the following statements is not a true statement?"

The following statement is not true.

"The relationship between Cat and Dog is an example of an appropriate use of inheritance."

I receive a lot of email from people that overlooked the word "not" in questions on my exam. Did you overlook that word?

Thank you again for using my exam.

Dan


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Chris Wash
Greenhorn

Joined: Aug 23, 2004
Posts: 16
Originally posted by Swati Udas:
class Leg{}
class Fur{}
abstract class Pet {
public abstract void eat();
public abstract void sleep();
}
class Dog extends Pet {
Leg leftFront = new Leg(), rightFront = new Leg();
Leg leftRear = new Leg(), rightRear = new Leg();
Fur fur = new Fur();
public Fur shed() {return fur;}
public void eat() {}
public void sleep() {}
}
class Cat extends Dog {
public void ignoreOwner() {}
public void climbTree() {}
}

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.

The answer is given e and explainetion is as follows:

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 special kind of a Dog. The goal of the OO paradigm is to develop software models that are accurate and reusable. If the software model is not accurate, then it probably is not reusable and the goals of the OO paradigm are not achieved. Code reuse and maintenance becomes increasingly difficult when inheritance is used to model inappropriate relationships. For example, suppose that somebody implements a herdSheep method in the Dog class. The Cat subclass would inherit the method and suddenly each instance of Cat would acquire the unwanted capability to make an attempt to herd sheep. It is difficult to imagine that a Cat would perform well in that role, so additional maintenance would be required to resolve the problem.

i think the xplaination and answer do not conform to each other the answer shud be D according to the above theory.Please clarify!


Make sure you read the question and answers carefully:

Which of the following statements is not a true statement?


The question is asking which one is not true, or false. a dog is-a pet, so it can't be D because that is true. a cat is-a pet would be an appropriate use of inheritance, but having an is-a relationship between a dog and a cat is not appropriate.

Try reading the answers first, then the question, and then the code they provide. Usually if you read the code first, you start thinking of an answer before you even read the question and don't really think twice about re-reading the question to make sure it's correct.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Originally posted by Dan Chisholm:

I receive a lot of email from people that overlooked the word "not" in questions on my exam. Did you overlook that word?Dan


The same thing happened so much at Sun on the real exams, that we are no longer allowed to use the word "not" in a question! On a very few number of answers we do use it, but we are required to capitalize it as in, "The getFoo() method will NOT run."

Apparently it is very easy for your brain to skip that word, thinking that it's "irrelevant" (yet another example of your brain *trying* to do you a favor against your wishes...).

cheers,
Kathy
Swati Udas
Ranch Hand

Joined: Aug 18, 2004
Posts: 121
Yes..I did miss the word "not"..
Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance- cat dog / pets