SIGN UP MEMBER LOGIN:    
ARTICLE

Why is composition favored over inheritance?

Posted by Gaurav Rawat Articles | C# Language February 21, 2011
Here you will see why composition is favored over inheritance.
Reader Level:


We had often heard that composition is better than inheritance. Why is it so? First of all how is one different from the other and what are the similarities in them?

Lets say we are writing simulation software for Rocket Launching systems which are to be supplied to different countries. Now these different countries can use them as they want it.
The code for our launching system is below:

public class Launcher
{
    public bool LaunchMissile()
    {
        Console.WriteLine("Missile launched");
        return true;
    }
}
public class SufraceToAirMissileLauncher:Launcher
{

 }

Now, country A uses this code to launch missile as follows:

static void Main(string[] args)
    {
        SufraceToAirMissileLauncher staLauncher = new SufraceToAirMissileLauncher();
        bool isLaunched =   staLauncher.LaunchMissile();
    }

This is how Inheritance is used. The various launchers can reuse the base Launcher class code to launch missile.

The same thing can be achieved by using Composition where base class functionality is encapsulated inside the main concrete class. The code for that is below:

public class SufraceToAirMissileLauncher
{
    private Launcher launcher = new Launcher();
    public bool LaunchMissile()
    {
        return launcher.LaunchMissile();
    }
}

The client UI code remains the same.

Now due to our superb code, our patented launching software had become famous and another country B wants to use it. But they had a condition that instead of launching the missile through base class they would want to get an instance of a missile. Now it's up to them what they want to do with it. They might add some nuclear material on it or modify it to increase its range or do whatever they might like. So another Missile object comes into the picture.

public class Missile
{
    private bool isLaunched;
    public bool IsLaunched
    {
        get { return isLaunched; }
        set { isLaunched = value; }
    }
    public Missile(bool isLaunched)
    {
        IsLaunched = IsLaunched;
    }
}

And the base class function has changed to:

public class Launcher
{
    public Missile LaunchMissile()
    {
        Console.WriteLine("Missile returned");
        return new Missile(true);
    }
}

Now it returns a missile instead of launching it. So now if we rely on inheritance, the client code of country A would break since the method signature has changed from what is being used in its UI.

However, if the country A had used composition instead, the code will not break. Only the derived class function would need to accommodate the new changed behavior of the base class. To accommodate this, we need to change our derived class code function "LaunchMissile" as:

public class SufraceToAirMissileLauncher
{
    private Launcher launcher = new Launcher();
    public bool LaunchMissile()
    {
        Missile missile = launcher.LaunchMissile();
        return missile.IsLaunched;
    }
}

Hence, the client code of country A would still work:

static void Main(string[] args)
    {
        SufraceToAirMissileLauncher staLauncher = new SufraceToAirMissileLauncher();
        bool isLaunched =   staLauncher.LaunchMissile();
    }

On the other hand country B which was insisting on getting a missile would still get missile from the base class.

So through this simple example we see how the composition is favored over inheritance to maintain compatibility and where there is a possibility that the functionality might change in future.
 

Login to add your contents and source code to this article
share this article :
post comment
 

It is a very well written article which makes it pretty easy to understand the concepts of Composition and Inheritance and why composition is better....Great Job!!! PS. Next time be a little more peaceful (missiles lol)....Just kidding.

Posted by Raja Krishnamurthy Feb 22, 2011

Good Work!

Posted by Sapna Feb 21, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor