GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark ""has a" relationship question" Watch ""has a" relationship question" New topic

"has a" relationship question

a hui
Ranch Hand

Joined: Oct 14, 2000
Posts: 39
I found this question in the Sun site. The answer is D and E. But I'm not sure why E is correct. To my option C is similar to E. Could someone explain this answer?
Thanks for your help

Which two demonstrate a "has a" relationship? (Choose two.)
A.public interface Person{ }
public class Employee extends Person{ }
B.public interface Shape{ }
public interface Rectangle extends Shape{ }
C.public interface Colorable{ }
public class Shape implements Colorable{ }
D.public class Species{ }
public class Animal{private Species species;}
E.interface Component{ }
class Container implements Component{
private Component[] children;
Dominic Steng�rd
Ranch Hand

Joined: Feb 05, 2001
Posts: 186
Hello a!
I look at it with the following perspective:
1. A "is a" relationship is declared by extending. For example:
An employee is a person
public class Employee extends Person
2. A "has a" relationship is declared by using member variables:
An employee has a car
public class Employee {
private Car eCar = new Car();
Dominic Steng�rd
Sun Certified Java Programmer

Dominic Steng�rd<br />Sun Certified Java 2 Programmer
a hui
Ranch Hand

Joined: Oct 14, 2000
Posts: 39
Hi Dominic,
I agree with you on that. But what about implementation of interfaces? Are they "is a" or "has a" relationship?
According to Sun's answer in (C), implementation is a "is a" relationship.But in (E) it says that implementation of an interface is a "has a" relationship.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi A,
'Interfaces' are Java's way of implementing multiple inheritance. A class can only have one direct parent, the one it 'extends', through implementing one or more Interfaces it can have additional behaviour; it can act like more than one object type.
For example, if you look at the API for the class <code>java.awt.CheckBox</code> you'll see it extends <code>java.awt.Component</code> and implements <code>java.awt.ItemSelectable</code>. A CheckBox 'is a' Component which behaves like all other ItemSelectable objects; it acts like both a Component and an ItemSelectable object therefore it 'is a' Component and 'is a' ItemSelectable object.
The code for answer C <code>public class Shape implements Colorable</code> tells you that Shape is an object that is also Colorable.
The code for answer E contains additional information:

From the definition we know that a Container is an object that is also behaves like a Component and which 'has an' Array object of type Component named 'children'.
Whenever an object 'contains' or 'has' another object, you have a 'has a' relationship. Answer D is correct because it also contains another object of type Species.
Hope that helps.

Jane Griscti
Sun Certified Java 2 Programmer

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Vijay pillai
Ranch Hand

Joined: Jan 10, 2001
Posts: 45
hello Jane,
This comes from java Cram.
Q)you are designing an application to give dietary advice to maximize the advantage of working in an object oriented language.
u have created a GenericFruit class that will be extended by classes representing different types of fruit.the following class fragment shows the class declaration and all of the instance variables in the GenericFruit class.
1. public class GenericFruit extends Object{
2. protected float avgWeight;
3. protected float caloriesPerGram;
4. String varietyName;
5.//class definition continues with methods
which of the following would be reasonable variable declarations for the Apple class that extends Genericfruit?
1. Image picture;
2. private float avgWeight;
3. private GenericFruit theFruit;
here the authour says that "3" is not a valid answer as there is no need for Apple to have GenericFruit because it is a GenericFruit by inheritance.
If we follow this principle,How can E be a valid answer.
am i wrong in the way i am looking at it,or am i missing something.
Vijay Pillai
a hui
Ranch Hand

Joined: Oct 14, 2000
Posts: 39
Thanks Jane for your help.
GeeCON Prague 2014
subject: "has a" relationship question