Difference between IS-A Relationship And HAS-A Relationship ?

 

 

IS-A relationship (Inheritance)

HAS-A Relationship(Composition)

1

It is known as Inheritance

HAS-A relationship is also known as Composition and aggregation

2

The main advantage of IS-A relationship is code reusability

There is no specific keyword to implement HAS-A relation but most of the time we are depending on the new keyword

3

By using extends keyword we can implement IS-A relationship

Composition (HAS-A)relationship are classes whose attributes are comprised of other classes

4

Whatever method parent has by default available  to the child and hence on the child reference we can call both parent and child method

the HAS-A relationship is association relationship for EX: A Student has a college in which he presently studies  A person has name

5

Whatever a method child has by default not available to parent and hence parent reference we can’t call child specific methods

Composition is a dynamic binding (run time binding) while Inheritance is static binding (compile time binding)

6

parent reference can be used to hold child object but by using that reference we can’t call child specific method but we can call the method present in the parent class

 Don’t use inheritance just to get code reuse A if you really want is to reuse code and there is no is-a relationship in sight use composition

7

parent reference can be used to hold child object but child reference can’t be used to hold parent object

Don’t use inheritance just to get at Polymorphism  a  if you really want is a polymorphism but there is no natural is-a relationship use composition with Interfaces

8

Total java API is implemented based on inheritance concept

9

The most common method which is applicable for any java objects is defined in object class and hence every class in java is the child class of object either directly or indirectly so that object class method by default available to every java class without rewriting due to this object class access as root for all java classes

The main Advantage of has a relationship is reusability of the code Ex

10

Throwable class defines the most common method which is required to every exception and error classes .Hence this class act as a root class for java exception hierarchy

class CAR{engine e=new engine(); A car has a engine reference }

11

Inheritance (IS-A) relationship are derived from child classes that inherit attributes and methods  from their parent class

Class engine { //engine  a specific functionality }

12

IS-A relationship is a kind of a basically  a generalization -specialization relationship in OOPs A human being is a kind of mammal . A mammal is kind of animal it is a relationship between a special class and general class The special class inherits the properties of the general class

 

13

 EX:    Apple IS-A fruit

Class Fruit{

   //This is fruit class or parent class

}

Class Apple extends Fruit{

   //This is child class or Child class

}

Ex: A Room HAS-A Table

Class Room{

Table table=new Table();

}

14

Ex : cat IS-A Animal

Class Animal{

}

Class Cat extends Animal{

 }

Ex:Cat HAS-A tail

Class Cat{

Tail tail=new Tail();

}

 

Comments

Blogs