DEV Community

loading...

Inheritance, Composition and Aggregation in C#

namvnngu profile image Nam Nguyen Updated on ・2 min read

In this post, I briefly introduce three main object-oriented programming terms for beginners in simple words. They are Inheritance, Composition and Aggregation. These programming techniques help developers write clean code, save many lines of code, refactor as well as maintain programs easily, where the developers have full control of the programs. After definition, I will list some main differences between composition and aggregation since they are quite similar.

1. Inheritance

In real life, inheritance is the action of passing property, debts or rights of parents to their children. Similarly, in programming, inheritance is the ability of a class to “copy” or acquire all properties and methods from another class. The class inheriting from another is a child class or derived class, and the class inherited by another class is the parent class. The inheritance helps derived classes extend their functionality as well as type, and create an “is-a” relationship between parent and child classes.

For example:

class Fighter
{
      private int _life;
      private int _attack; 
      public Fighter(int life, int attack)
      {
          _life = life;
          _attack = attack;
      }

      public int GetLife() 
      { 
         return _life;
      }
}

class Soldier : Fighter
{
      public Soldier(int life, int attack): base(life, attack){}
}

The above example illustrates that Fighter is a parent class and Soldier is a child class. In the child class, you do not need to repeat all attributes and methods defined in the parent class. Apparently, the Soldieris-a” Fighter. Let me create instances of them and make a call to GetLife() method.


class Program
{
    static void Main(string[] args)
    {
        Soldier soldier = new Soldier(10, 1);
        int life soldier.GetLife();
        Console.WriteLine(life); // 10
    }
}

2. Composition and Aggregation

Composition and aggregation are programming techniques which allow a class to “contain” one or more objects of other classes to form a big object doing some specific functionalities. The container is the superclass and the classes contained by the superclass are subclasses. Therefore, the superclass and subclasses have a “has-a” relationship.

However, composition and aggregation must have some differences to distinguish between them. Let’s see the following examples:

Composition:

class ChairLeg
{
    public void Material() 
    {
       Console.WriteLine("Wood");
    }
}
class Chair
{
    private ChairLeg _leg;
    public Chair(ChairLeg leg)
    {
        _leg = leg;
    }
}

Aggregation


class Chair
{
    public void Material() 
    {
        Console.WriteLine("Wood");
    }
}
class DinnerTable
{
    private Chair _chair
    public Chair(Chair chair)
    {
        _chair = chair;
    }
}

The two examples above show “has-a” relationship, DinnerTable “has-a” Chair around and Chair “has-a” ChairLeg (obviously, a chair has 4 four legs, but here is just an example for you to understand the concept). Therefore, DinnerTable has a loose relationship with Chair because DinnerTable can not be useless or destroyed if it does not have a Chair object. In other words, DinnerTable exists independently from Chair. However, oppositely, Chair has a solid relationship with ChairLeg, which means that Chair cannot exist independently without ChairLeg.

As a result, the main difference is independence. In composition, subclasses' life cycle depends directly on superclass’s life cycle and vice versa. In aggregation, the subclasses and superclass have their own life cycle.

3. Conclusion

This post is a brief explanation of inheritance, composition and aggregation in object-oriented programming for beginners. If you find any mistakes in my post or give my feedback, please comment below. I am welcome to your responses.

Discussion (4)

pic
Editor guide
Collapse
codecoachnz profile image
Olaf Thielke

Thank you for your helpful article. I am still a bit confused about one thing. Your code examples of chair/table and chair/chairleg are effectively the same yet they seem to be archetypal for the different situations. It would be nice if the code examples would more clearly show how the with composition the chairleg cannot exist without the chair. However, it seems to me that it can. It is being created before the chair and then passed into the chair constructor. So at least momentarily it can live without the chair. As a suggestion, maybe if chair instantiated the chairleg its constructor it would be more composition-like? No?

Collapse
namvnngu profile image
Nam Nguyen Author

Yes. You're right. That's what I meant, ChairLeg can exist without Chair, but I did not mention it quite clearly. General speaking, I want to emphasize that "In composition, subclasses' life cycle depends directly on superclass’s life cycle and vice versa. In aggregation, the subclasses and superclass have their own life cycle.". Thanks for your comment. Have a good day!

Collapse
johnt1999 profile image
JohnT1999

Nice job ! very helpful

Collapse
toanchungg profile image
toanchungg

great article