• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HAS-A relationship?

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When can we say that 2 classes have a HAS-A relationship???
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Car object HAS-A(n) Engine.

The Clock object HAS-A Face.
The Dog object HAS-A Tail.
The Book object HAS-A Title.
The Person object HAS-A Name.
The Car object HAS-A Driver.

Millions of possibilities.
 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For java this is where one type of object is an instance member of another type of object. This is also called composition

For example Consider two Classes Employee and Address. The Employee class has a member private Address add1;

The Employee class and Address class has a, HAS-A relationship. The Employee class has an Address.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ex:
class c
{
public int id;
}
class A
{
C c=new C();
}

In this case, what is the relation between class A and Class C???
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abhi vijay:
In this case, what is the relation between class A and Class C???


None. You haven't got a class "C" anywhere. There is a class "c" but you aren't using that. If you get rid of the capitalisation error you would probably have a HAS-A relationship.

And please use correct indentation and the CODE button; it makes the code much easier to read.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain how they have a HAS-A relationship??
 
Thomas Thevis
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhi,

a HAS-A relationship between two classes usually means that an instance of one of the classes holds an instance variable of the type of the other variable. Thus, in your code from before with the capitalization error corrected as Campbell suggested

A HAS-A C.
Hope this helps,
Thomas
 
krmgroups
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is-a = inheritance.
has-a = composition
eg:
class A extends class B,

then 'A' is a 'B' . i.e say class dog extends animal then dog is a animal.

and if class A has B obj then A has a B. say

class dog{
Leg leg;//here Leg is a class name
//Leg's object(leg) has holds the dog class
}

then class dog has a class Leg.

or
You "are a" (is-a) research-challenged person.
You "have a" (has-a) resistance to searching the internet or books.

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vinoth,

Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name here. Thanks!
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Thevis:
a HAS-A relationship between two classes usually means that an instance of one of the classes holds an instance variable of the type of the other variable

Small addition: arrays or Collections (List, Set, etc) of the variable type also specify a HAS-A relationship, even though it's grammatically not HAS-A but more HAS-MULTIPLE
 
krmgroups
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A LITTLE MORE: HAS-A:
what is 'is a' and 'has a' relationship:

The is a relationship is expressed with inheritance and has a relationship is expressed with composition.
Both inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for code reuse are class inheritance and object composition.

e.g.,

is a --- House is a Building



has a -- House has a bathroom


Inheritance Vs Composition

Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses
extends key word. Composition: is used when House has a Bathroom. It is incorrect to say House is a Bathroom.

Composition simply means using instance variables that refer to other objects. The class House will have an instance variable

[edit]Add code tags. CR[/edit]
[ September 30, 2008: Message edited by: Campbell Ritchie ]
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot..i understood the concept.
 
Nitesh Nandwana
Ranch Hand
Posts: 115
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krmgroups wrote:A LITTLE MORE: HAS-A:
what is 'is a' and 'has a' relationship:

The is a relationship is expressed with inheritance and has a relationship is expressed with composition.
Both inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for code reuse are class inheritance and object composition.

e.g.,

is a --- House is a Building



has a -- House has a bathroom


Inheritance Vs Composition

Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses
extends key word. Composition: is used when House has a Bathroom. It is incorrect to say House is a Bathroom.

Composition simply means using instance variables that refer to other objects. The class House will have an instance variable

[edit]Add code tags. CR[/edit]
[ September 30, 2008: Message edited by: Campbell Ritchie ]


please don't post content from other sources if it is not your then mention source of it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic