When to use Inheritance and Composition

By Sateesh Arveti Blogs | Coding Best Practices Mar 02, 2009
Inheritance and Composition are two ways to re-use the code.We will see when to use which technique.
Inheritance defines a new class in terms of an existing class. A subclass inherits both data and behavior from its super class. Object Composition defines a new class as containing an instance of one or more existing classes. We should use Class Inheritance only if instances of the new class can be used in all situations where the existing class can be used.Class Inheritance not inappropriate if the subclass needs to stub out behaviour implemented by the superclass. We should use Object Composition if instances of the new class do not require the protocol of the superclass.