aspose file tools*
The moose likes Beginning Java and the fly likes HAS-A relationship? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HAS-A relationship?" Watch "HAS-A relationship?" New topic
Author

HAS-A relationship?

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
When can we say that 2 classes have a HAS-A relationship???
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
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

Joined: Aug 05, 2008
Posts: 350
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

Joined: Sep 16, 2008
Posts: 509
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

Joined: Oct 13, 2005
Posts: 39415
    
  28
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

Joined: Sep 16, 2008
Posts: 509
Can you explain how they have a HAS-A relationship??
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
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


SCJP 5.0, SCJD in progress
krmgroups
Greenhorn

Joined: Mar 28, 2008
Posts: 6
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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!


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
krmgroups
Greenhorn

Joined: Mar 28, 2008
Posts: 6
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

Joined: Sep 16, 2008
Posts: 509
Thanks a lot..i understood the concept.
Nitesh Nandwana
Ranch Hand

Joined: Jun 07, 2011
Posts: 115

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HAS-A relationship?