When to use Inheritance and Composition

By Sateesh Arveti 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.
Sateesh Arveti

I hold Bachelors degree in Computer Science along with MCSD,MCTS and MVP for the year 2009. Areas of Interest: C#, WPF, WF, silverlight, ASP.NET, Oracle and SQL Server.

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs