DEV Community

Adrián Bailador
Adrián Bailador

Posted on

Inheritance and Composition in C#

Inheritance

Inheritance is a programming mechanism that allows you to derive a class from another class, forming a hierarchy of classes that share a set of attributes and methods. The class from which inheritance is derived is called the base class, and the class that inherits from the base class is called the derived class. Inheritance forms an "is-a" relationship. For example, a Dog is an Animal.

Here is a simple example of inheritance in C#:

public class Animal
{
    public string Name { get; set; } = "Animal"; 
    public void MakeSound() 
    {                    
        Console.WriteLine("The animal makes a sound.");
    }
}
// child
public class Dog : Animal  
{
    public string Breed { get; set; } = "Golden";
}
Enter fullscreen mode Exit fullscreen mode

Advantages of Inheritance

  1. Code Reusability: Inheritance allows for the reuse of code by defining common attributes and methods in a base class that are then inherited by derived classes.
  2. Code Organisation: It helps in organising the code by grouping similar classes together in a class hierarchy.
  3. Extensibility: Functionality in the base class can be extended in the derived class without modifying existing code, adhering to the open/closed principle.
  4. Polymorphism: It allows derived classes to be treated as instances of the base class, making the code more general and reusable.

Disadvantages of Inheritance

  1. Strong Coupling: Inheritance creates a strong coupling between the base class and the derived class, making the code more difficult to modify.
  2. Rigid Hierarchy: It creates a rigid class hierarchy that may not be suitable for all situations. In C#, a class can only inherit from one base class.
  3. Propagation of Changes: Changes in the base class can affect all derived classes, which can be risky and require careful management.
  4. Depth of Hierarchy: Deep inheritance hierarchies can be difficult to follow and understand, leading to less readable and harder to maintain code.

Composition

Composition, on the other hand, is a technique where a class contains instances of other classes to utilise their functionalities. This "has-a" relationship is more flexible than inheritance and is used to model objects that contain or are composed of other objects. For example, a Dog has a Collar.

Here is a simple example of composition in C#:

public class Collar
{
    public void Open()
    {
        Console.WriteLine("The collar opens");
    }
}

public class Dog
{
    private Collar _collar = new Collar();

    public void WearCollar()
    {
        _collar.Open();
        Console.WriteLine("The dog wears a collar");
    }
}
Enter fullscreen mode Exit fullscreen mode

Advantages of Composition

  1. Flexibility: It allows for changing the behaviour of an object at runtime by changing its components.
  2. Decoupling: Promotes stronger decoupling between classes, making the code easier to modify and maintain.
  3. Component Reuse: Facilitates the reuse of individual components in different classes.
  4. Unit Testing: Makes classes easier to unit test as components can be isolated and tested separately.

Disadvantages of Composition

  1. More Code: It requires more code than inheritance, as it involves creating instances of components and managing their lifecycle.
  2. Complexity: Increases the complexity of the code, as the relationships between components need to be managed.
  3. Maintenance Overhead: Managing dependencies and the lifecycle of objects can introduce maintenance overhead.

How to Choose Between Inheritance and Composition

The choice between inheritance and composition depends on the specific needs of your software. Here are some general guidelines:

  • Favour Composition Over Inheritance: This is a commonly accepted design principle. Composition is generally more flexible and provides better encapsulation than inheritance.
  • Use Inheritance to Model "Is-A" Relationships: If there is a clear "is-a" relationship between the classes, inheritance might be the right choice. For example, a Dog is an Animal.
  • Use Composition to Model "Has-A" Relationships: If a class needs to utilise functionality from another class but there is no "is-a" relationship, composition is the right choice. For example, a Dog has a Collar.

Practical Example of Decision

Suppose you are developing a system to manage different types of animals. Consider the following classes:

public class Animal
{
    public string Name { get; set; }
    public void MakeSound() => Console.WriteLine("The animal makes a sound.");
}

public class Cat : Animal
{
    public void Meow() => Console.WriteLine("The cat meows.");
}

public class Dog : Animal
{
    public Collar Collar { get; set; } = new Collar();
    public void Bark() => Console.WriteLine("The dog barks.");
}

public class Collar
{
    public void Open() => Console.WriteLine("The collar opens");
}
Enter fullscreen mode Exit fullscreen mode

In this case:

  • We use inheritance to represent that a Cat and a Dog are types of Animal.
  • We use composition to indicate that a Dog has a Collar.

Summary

  • Inheritance is useful for "is-a" relationships and for reusing code in a class hierarchy.
  • Composition is more flexible and is used for "has-a" relationships, allowing for greater modularity and component reuse.

The choice between inheritance and composition depends on what you are looking for in your design and how you expect your classes and their responsibilities to evolve.

Top comments (0)