The concept of inheritance is based on “is a” relationship. Composition is based on “has a” relationship. Both of these two concepts allow placing sub objects into your class. Below image explains this key relationship.
Inheritance Vs Composition
Inheritance is based on one direction concept. You can not apply it in reverse. Extends keyword is used to specify inheritance.
Composition is used in cases in which