java4all@1986 java. Powered by Blogger.

What i difference between "is a " relationalship and "has a" relationalship?

>> Sunday, May 29, 2011

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.


Inheritance [ is a ] Vs Composition [ has a ]
Building
Bathroom
House

Building to House is a relationship;;
House to Bathroom has a relationship
class Building{
.......
}
class House extends Building{              //is a [House is a Building]
.........
}

class House {
Bathroom room = new Bathroom() ;
....
public void getTotMirrors(){           //has a bathroom
room.getNoMirrors();
....
}
}
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, which refers to a Bathroom object

0 comments:

Post a Comment

FaceBook Login

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

HTML/JAVASCRIPT

Total Pageviews

STATCOUNTER

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP