This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Qn from Sun's samples

 
Praveen Zala
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls anyone answer this with Xplanations:
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;
}

TIA
Praveen Zala
 
asim wagan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is (d) & (e)
 
Praveen Zala
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that it is d and e.....I want some detailed
explanations....regarding the same....
(P.S - I know that Sun's sitehas teh answers, but I need
to know the explanation)
 
Kathy Rogers
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A 'has a' relationship indicates that a class 'has a' a certain property - that is a member variable. An 'is a' relationship indicates that a subclass 'is a' type of the superclass. The statement 'A car is a vehicle' implies
Class Car extends Vehicle
But the statement a car has a steering wheel implies:-
class Car{
SteeringWheel theWheel;
}
where SteeringWheel is a class member variable.
So if we look at your example:-
a,b,c all involve empty classes and interfaces - no class member variables.
But in d, the Animal class has a class member variable, species.
And in e, the class Container (which incidentally 'is a' Componenet) has a class member variable, children.
Hope this makes things a bit clearer.
Kathy
 
Bharatesh H Kakamari
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer : http://www.javaranch.com/ubb/Forum24/HTML/006021.html

a)public interface Person{ }
public class Employee extends Person{ }
This is wrong ! A class cannot extends an interface
b)public interface Shape{ }
public interface Rectangle extends Shape{ }
This is is-a relationship [b]
c)public interface Colorable{ }
public class Shape implements Colorable{ }
[b] This is a realize relationship

d)public class Species{ }
public class Animal{private Species species;}
This is a has-a relationship.
e)interface Component{ }
class Container implements Component{
private Component[] children;
}
This is a has-a relationship
So the correct answers are d and e.

HTH
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic