First, some background on the meaning of inheritance and composition.
About inheritance In this article, I'll be talking about single inheritance through class extension, as in:
class Fruit {
//...
}
class Apple extends Fruit {
//...
}
In this simple example, class Apple is related to class Fruit by inheritance, because Apple extends Fruit. In this example, Fruit is the superclass and Apple is the subclass.
I won't be talking about multiple inheritance of interfaces through interface extension. That topic I'll save for next month's Design Techniques article, which will be focused on designing with interfaces.
Here's a UML diagram showing the inheritance relationship between Apple and Fruit:

Figure 1. The inheritance relationship
About composition By composition, I simply mean using instance variables that are references to other objects. For example:
class Fruit {
//...
}
class Apple {
private Fruit fruit = new Fruit();
//...
}
In the example above, class Apple is related to class Fruit by composition, because Apple has an instance variable that holds a reference to a Fruit object. In this example, Apple is what I will call the front-end class and Fruit is what I will call the back-end class. In a composition relationship, the front-end class holds a reference in one of its instance variables to a back-end class.
The UML diagram showing the composition relationship has a darkened diamond, as in:

Figure 2. The composition relationship
When you establish an inheritance relationship between two classes, you get to take advantage of dynamic binding and polymorphism. Dynamic binding means the JVM will decide at runtime which method implementation to invoke based on the class of the object. Polymorphism means you can use a variable of a superclass type to hold a reference to an object whose class is the superclass or any of its subclasses.
One of the prime benefits of dynamic binding and polymorphism is that they can help make code easier to change. If you have
a fragment of code that uses a variable of a superclass type, such as Fruit, you could later create a brand new subclass, such as Banana, and the old code fragment will work without change with instances of the new subclass. If Banana overrides any of Fruit's methods that are invoked by the code fragment, dynamic binding will ensure that Banana's implementation of those methods gets executed. This will be true even though class Banana didn't exist when the code fragment was written and compiled.
This article is too good.Any one who wants to get a clear idea oBy Anonymous on January 29, 2010, 6:21 amThis article is too good.Any one who wants to get a clear idea on inheritance Vs Composition,please stop by 'n' have a look.Thanks for such a nice article!!!
Reply | Read entire comment
This is right answer how to choose between Abstract class and InBy Anonymous on December 11, 2009, 5:18 amThis is right answer how to choose between Abstract class and Interface.
Reply | Read entire comment
Awesome articleBy Anonymous on December 7, 2009, 3:39 amVery very very good article. I got the clear understanding about inheritance vs composition. Thanks a lot writing such nice article. keep continue writing such...
Reply | Read entire comment
Cool article! This site is one of the best for java articles andBy Anonymous on December 4, 2009, 12:26 amCool article! This site is one of the best for java articles and getting answers to common problems. Thanks a lot!!!
Reply | Read entire comment
NiceBy Anonymous on November 14, 2009, 9:42 amExcellent article. Thanks! Gotta hit my UML books more.
Reply | Read entire comment
View all comments