• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HAS-A relationship?

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When can we say that 2 classes have a HAS-A relationship???
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain how they have a HAS-A relationship??
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot..i understood the concept.
 
Ranch Hand
Posts: 115
Firefox Browser Notepad Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic