Contents :
- IS-A relationship in OOP (Inheritance)
- Has-A relationship (Association)
- Different type of Association : Aggregation and Composition
- Example of each type
- SCJP exam objective 5.5
IS-A (Inheritance) :
In Object oriented programming, IS-A relationship denotes “one object is type of another”. IS-A relation denotes Inheritance methodology.
In Java, Inheritance can be implemented with extends (in case of class) and implements(in case of interface) keywords.
A simple example of IS-A relation : Dell IS-A laptop.
Unit 2 (Prog 6) : Inheritance in JAVA contains some detailed information about inheritance.
If you will check instanceof condition “Dell instanceof Laptop”, It is true since Dell IS-A Laptop.
class Laptop { // Code for Laptop class goes here. } interface Formatable { // Members of Interface. } class Dell extends Laptop implements Formatable { // More code related to Dell goes here. // Dell class will inherit all accessible members of Laptop class. // Dell IS-A Laptop. // And Dells class also implements all method of Formatable interface, since // Dell is not an abstract class. // so Dell IS-A Formatable. }
Note that Object class will be there always on top in every inheritance Hierarchy, So every class holds IS-A relationship with Object class and instance of Object test is always true for all class.
Below image includes inheritance hierarchy for given example.
Has-A (Association) :
In Object orientation design, We can say “class one is in Has-A relationship with class B if class A holds reference of Claas B”.
By this reference of class B, A can access all properties of class B which are allowed.
Some off topic talk, To use Inheritance and Aggregation in your code is a good programming practice. It reduces the code duplication and increases reusability.Example :
class HardDisk { public void writeData(String data) { System.out.println("Data is being written : " + data); } } class UseDell { // segate is referece of HardDisk class in UseDell class. // So, UseDell Has-A HardDisk HardDisk segate = new HardDisk(); public void save (String data) { segate.writeData(data); } }
Aggregation v/s Composition :
So this is a famous confusing topic, Here we are goes to explain this as simple as possible.
Has-A relationship or Association can be divided in two types :
Aggregation and Composition
Aggregation :
Consider above example : HardDisk class and UseDell class.
In the example, UseDell HAS-A HardDisk.
If Laptop stops working somehow, you can remove harddisk from that and connect to other laptop, This is Aggregation.
In aggregation container class (UseDell) and referenced class (HardDisk) can have their independent existence.
Composition :
Now for Composition, consider two different class House and Kitchen.
class House { Kitchen kitchen = new Kitchen(); // More code for House class. } class Kitchen { // code of Kitchen class. }
If House gets destroyed Kitchen also will be destroyed with that House, This is composition.
In composition reference class (Kitchen) can not exist if container class (House) gets destroyed.
Reference class has its own existence even if Container class gets destroyed in Aggregation.
Reference class can exist only with Container class in Composition.
public class TestRelationship { public static void main(String[] args) { Dell oDell = new Dell(); if (oDell instanceof Laptop) { System.out.println("Dell IS-A Laptop."); } if (oDell instanceof Formatable) { System.out.println("Dell IS-A Formatable."); } // Test HAS-A new UseDell().save("This will be stored in segate."); } }
You should have separated inheritance and composition in 2 separate posts.
A separate article available on Inheritance.
http://java8.in/unit-2-prog-6-inheritance-in-java/
Excellent .Thanks a lot.
Simply Superb.
The examples used are very much understanding
Well this explanation is amazing.
Very simple explanation but effective. Keeping both to gather help us to understand it better relations between objects.
it would be better to give reallife example for Has-A-Relation
I want real life example for has a relationship