• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

is-a relationship

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
..In all honesty though, Interfaces are the Java way to get around Multiple Inheritance.. So maybe it is an "is a" relationship..
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 can't renounce my name. It's on all my stationery! And hinted in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic