Tuesday 22 July 2014

Java - IS-A and Has-A Relationship or Inheritance vs Association (Composition and Aggregation)

IS-A relationship

Inheritance is provide us the re-usability feature. Let's take one example we have a Human Category, so either boy can be human or girl can be human and each human has some common activity like eat(), walk(), see() apart from common activity boy has some activity like run() similarly girl has skip() and cookFood().
see more detail on below diagram-



So Boy and Girl both denoted by Human in common. and this is called inheritance and this relationship human--boy  and human --girl called IS-A relationship. so in short you can say Boy IS-A Human similarly Girl Is-A Human. so IS-A relationship always give re-usability feature.And In java we can achieve either by extends(In case of class) keyword or implements(in case of interface). 

In UML we can use arrow to show relationship between parent and child.
in this case we will say Boy-------------->Human

Has-A relationship (Association)

Has-A means an instance of one class “has a” reference to an instance of another class or another instance of same class.There is no specific keyword to implement HAS-A relationship but mostly we are depended upon “new” keyword. 
It can be two type-
  1. Aggregation
  2. Composition
Aggregation means weak has-a relationship between object. Let take a example if you have car and car has a headlight, and here headlight object removed from the car then also car can be run. i.e. in this type of relationship both object are loosely associated. In other word, It is an association that represents a part-whole or part-of relationship. As a type of association, an aggregation can be named and have the same adornments that an association can. However, an aggregation may not involve more than two classes.
In UML it is graphically represented as a hollow diamond shape on the containing class end of the tree with a single line that connects the contained class to the containing class. The aggregate is semantically an extended object that is treated as a unit in many operations, although physically it is made of several lesser objects

class Car {
          Headlight headlight = new Headlight();
           ............
          ...............
}


Composition means strong has-a relationship between object. for example car and engine, if engine is removed then car can not run. That means if both object are tightly associated hence this makes strong relationship between object.In other word Composition usually has a strong life cycle dependency between instances of the container class and instances of the contained class(es): if the container is destroyed, normally every instance that it contains is destroyed as well. (Note that, where allowed, a part can be removed from a composite before the composite is deleted, and thus not be deleted as part of the composite.)
The UML graphical representation of a composition relationship is a filled diamond shape on the containing class end of the tree of lines that connect contained class(es) to the containing class.

class Car {
   Engine engine = new  Engine();
   ...................
   ..................

}

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. If you're a novice, you can easily get a scan tool for your car. Alternatively, a professional can purchase a scanner for his or her home use. Author is an expert of total car diagnostics, go here for more interesting information.

    ReplyDelete
  3. Java Class By Rk: Java - Is-A And Has-A Relationship Or Inheritance Vs Association (Composition And Aggregation) >>>>> Download Now

    >>>>> Download Full

    Java Class By Rk: Java - Is-A And Has-A Relationship Or Inheritance Vs Association (Composition And Aggregation) >>>>> Download LINK

    >>>>> Download Now

    Java Class By Rk: Java - Is-A And Has-A Relationship Or Inheritance Vs Association (Composition And Aggregation) >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete