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 Java, then go through this Java tutorial by Intellipaat for more insights.