This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes is-a question 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 "is-a question" Watch "is-a question" New topic
Author

is-a question

Howard Kung
Greenhorn

Joined: Jul 22, 2002
Posts: 24
Hi all,
I have question about the "is-a" relationship.
Given:
public interface HeavelyBody {String describe();}
class Star{
String starName;
public String describe() {return "star" + starName;}
}
class Planet extends Star{
String name;
public String describe(){
return "planet " + name + " orbiting star " + starName;}
}
Then the question asks to select all the valid answers given. One of the answer given is "The use of inheritance is justified, since planet is-a star". But according to the answer at the end of the book, "The use of inheritance in this code is not justifiable, since conceptually, a planet is-not-a star".
I don't understand why "planet is-not-a star" since Planet extends Star.
any idea?
Thanks in advance


Howard Kung<br />SCJP 1.2<br />SCWCD<br />IBM Certified Specialist: WebSphere 4.0
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Howard
What book is this from? In the code you entered a Planet is indeed a Star. Maybe it is a typo? You have an interface, HeavelyBody, that neither class implements so maybe the book meant HeavelyBody


Dave
Howard Kung
Greenhorn

Joined: Jul 22, 2002
Posts: 24
Thanks Dave. The book I have is "A Programmer's Guide to Java Certification: A comprehensive Primer" by Khalid A. Mughal & Rolf W. Rasmussen.
The question is #6.26 (pg. 220).
Howard Kung
Greenhorn

Joined: Jul 22, 2002
Posts: 24
P.S
It's "HeavenlyBody".
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Howard,
When that author states that a planet is not a star, he is stating the opinion that a planet like Earth is not a specialized instance of a star. I am no astronomer, so I don't have an opinion.
Let's look at an example that is less controversial. Does it make sense to declare a class Dog as an extension of a class Cat? Obviously not because a dog is not a cat. In your example, just replace planet and star with dog and cat if you want to agree with the author's view.


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

Joined: Mar 22, 2002
Posts: 1055
And I thought this thread was about Seven of Nine
If I remember correctly, Mughal has two questions of these sort. Can you post them up again?
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Ohhhh I get it now.
Harold, the author is trying to get the reader to determine if this is a valid use of inheritance. He's not asking about the actual code written and how to interpret it.
Basically he is saying here is this code, is it good code? does it make sense? And the answer is of course, no! Because, as Dan said, a Planet is not a Star (although if we got into it we could probably have a pretty good arguement one way or the other).
In the explaination you mentioned from the book where he says a planet is a star, he is probably just explaining the meanings of the code, in that, regardless of real life, this code, as written, is saying that a planet is a star, even though in real life a planet is not a star.
sorry about the confusion I thought this was from a code standpoint not a design standpoint. Reading Dan's post cleared it up for me.
Hope that helps
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Indeed, this question refers to whether the use of inheritance is justifiable, not just legal. Let's use a simpler example, and we'll move on from there.
Let's say that we already written a class Cat like this:

Now, let's say that we want to write a class Dog. Well, a Dog should have a name, right? Cat has a name and methods to get and set it, so, it would save us some extra coding to simply extend Cat, right?

Now, without really writing any extra code, our Dog class has a gettable and settable name. Of course, this use of inheritance is perfectly legal. Great, right?
However, the down side of this is that, according to this application, a Dog "is a" cat. Well, that doesn't make much sense, does it? A Dog isn't really a Cat, but, in the universe that our application lives in, it is.
This is faulty inheritance. Not because it isn't legal inheritance, but because it is counter-intuitive. One of the basic concepts behind OO programming is that you want to model real-world entities so that your application behave similarly to objects in the real world. If you go around making a Dog a Cat or vice-versa, you're just asking for trouble because all of your applications will require anyone that writes (or extends it) to adopt some new logic.
In this case, the only methods Cat has referred to its name. What if the Cat class looked like this:

Does it still make sense for a class named Dog to extend Cat? Then our Dog would be able to Purr. Once again, this would be legal, but it doesn't make much sense.
Instead, I'd recommend using a third class (or interface) to abstract away the commonalities of the two classes. Let's make a NamableEntity class:

Now, we can just have Dog and Cat extend this class:

Now, a Dog is not a Cat not vice-versa. Instead, both a Cat and a Dog are NamableEntities. That makes much more sense, doesn't it? You can apply a very similar argument to the Star and Planet program above.
While studying for this exam, it is very easy to get caught up in what is legal and what isn't legal. After all, almost the entire exam is about syntax. It is important, however, to remember what is right, not just what is legal.
I hope that helps,
Corey


SCJP Tipline, etc.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
Originally posted by Dave Vick:
Ohhhh I get it now.
Because, as Dan said, a Planet is not a Star (although if we got into it we could probably have a pretty good arguement one way or the other).

I'm not an astromomer either, but how could you argue that a planet is a star? And did I just move this thread to Meaningless Drivel"?
At your service,
M


Java Regular Expressions
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Max Habibi:

I'm not an astromomer either, but how could you argue that a planet is a star? And did I just move this thread to Meaningless Drivel"?
At your service,
M

You could argue that when people look up into the night sky they see Jupiter and Saturn and assume they are looking at stars. Even so, I have no real opinion on the issue.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is-a question
 
Similar Threads
Another question from Khalid
KAM India Edition Question 6.27
Help needed on Inheritance
why would this code run without an error?
looking at this code, why Planet class is not a Star class?