Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

Can anyone tell me what is the IS-A and HAS-A relationship in Java and when do we use them?

1 Answer

0 votes
by (19.7k points)

Inheritance has an IS-A relationship. It shows that the child class is a type of parent class. Here, the type of object is known at the compile time (static binding). For example, a monkey(child class)  is an animal. It will inherit from its parent class ‘animal’.

Class Monkey extends Animal {

}

The composition is a HAS-A relationship. It creates instances which refer to other instances objects. Here, the type of object is determined at the run time(dynamic binding)   Example: a house has a bedroom. It’ll create a Class House and in that it will create an instance of type bedroom. 

class House{

 Bedroom object = new Bedroom();

}

If you want to learn more about Javathen go through this Java tutorial by Intellipaat for more insights.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...