aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes is-a relationship 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 "is-a relationship" Watch "is-a relationship" New topic
Author

is-a relationship

Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
When exactly do we say there is an is a relationship?
question from a mock:
Where will be a 'is a' reln
A. interface Person
class Employee implements Person
B. interface Shape
interface Rectangle extends Shape
C. interface Component
class Object implements Component
{Component[] Component;}
Ans given is B.
I thought all were in 'is-a' relations, cause "implements" also counts as 'is-a'.
Thanks.


Anup Engineer
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi Anup,
Whenever a class extends another class it is 'is a' relationship.

We can use an object of class Dog wherever we use an object of superclass Mammal. This defines an 'is a ' relation. Dog 'is a' mammal.

Has-a relationship is about an instance of the class and its variables.
Now an instance of Mammal 'has a' variable for its weight and to know whether it has tail or not.
Hope this helps,
Vanitha.
[ March 30, 2002: Message edited by: Vanitha Sugumaran ]
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
Well, the way I see it..
public class Person implements Runnable
A person isnt a Runnable.. it just has that ability... So implements is more of a "has the ability".. to me anyways.. People have the ability to walk.. but that doesnt make them walkers does it.. wait.. maybe it does ..
Joshua Kueck
Ranch Hand

Joined: Mar 14, 2002
Posts: 71
..In all honesty though, Interfaces are the Java way to get around Multiple Inheritance.. So maybe it is an "is a" relationship..
JoelDDrechsler
Greenhorn

Joined: Mar 16, 2002
Posts: 3
The "has a" relationship is for nesting with inheritance remember that interfaces are just another kind of inheritance at the end of the day.
So class Car has a class Engine.
The "is a" relationship is used mostly for is something an instance of a class? So a Car is an Engine? No. A BMW is a car. Yes. You could use this for relations in hieredity as in a cat is a mammal. So this is good for determining if something is on the "inheritance chain" of the class.
Hope this clairifies.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
I thought all were in 'is-a' relations, cause "implements" also counts as 'is-a'.

I agree with you here. IMHO as long as an inheritance relationship holds between class to class, interface to class, and interface to interface, an is a relationship holds.
A. interface Person
class Employee implements Person

Employee e = new Employee();
// (e instanceof Person)==true
B. interface Shape
interface Rectangle extends Shape

// Class MyGrid implements Rectangle
Rectangle r = new MyGrid();
// (r instanceof Shape)==true
C. interface Component
class Object implements Component{Component[] Component;}

???
There is already a java.lang.Object class. Okay, assuming you made your own Object class,
com.anup.Object obj = new com.anup.Object();
// (obj instanceof Component)==true
The has a relationship implies aggregation, e.g.,
class Employee implements Person
{
String employeeNumber;
// etc
}
Employee e has an employeeNumber, but employeeNumber instanceof Employee (no!),
employeeNumber instanceof Person (no!)
It is legal to do this (but it might get confusing )
interface Person
{
// etc.
}
class Employee implements Person
{
Person p;
// etc.
}
in which case an Employee e is a Person, while at the same time has a Person p.
-anthony
[ March 30, 2002: Message edited by: Anthony Villanueva ]
[ March 30, 2002: Message edited by: Anthony Villanueva ]
Neelima Rao
Greenhorn

Joined: Feb 24, 2002
Posts: 26
I dont remember where I read it from, but whenever I come across an "is like" relationship, I assume it as implementing an interface. Similarly "is" for extends.
For example, "Object is like a Component" means Object implements Component.
Is it correct? Could someone please clarify? I know very little about Object-oriented design, so I'm not sure my question makes sense at all.
TIA
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
is-a is used to show that something is on the hierarchy with the other thing. So if instanceof will return true, then it is-a.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Joshua Kueck:
Well, the way I see it..
public class Person implements Runnable
A person isnt a Runnable.. it just has that ability...
A Person is-a Runnable object.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is-a relationship