Inheritance and Composition in C#
Adrián Bailador
Posted on May 24, 2024
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";
}
Advantages of Inheritance
- 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.
- Code Organisation: It helps in organising the code by grouping similar classes together in a class hierarchy.
- Extensibility: Functionality in the base class can be extended in the derived class without modifying existing code, adhering to the open/closed principle.
- Polymorphism: It allows derived classes to be treated as instances of the base class, making the code more general and reusable.
Disadvantages of Inheritance
- Strong Coupling: Inheritance creates a strong coupling between the base class and the derived class, making the code more difficult to modify.
- 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.
- Propagation of Changes: Changes in the base class can affect all derived classes, which can be risky and require careful management.
- 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");
}
}
Advantages of Composition
- Flexibility: It allows for changing the behaviour of an object at runtime by changing its components.
- Decoupling: Promotes stronger decoupling between classes, making the code easier to modify and maintain.
- Component Reuse: Facilitates the reuse of individual components in different classes.
- Unit Testing: Makes classes easier to unit test as components can be isolated and tested separately.
Disadvantages of Composition
- More Code: It requires more code than inheritance, as it involves creating instances of components and managing their lifecycle.
- Complexity: Increases the complexity of the code, as the relationships between components need to be managed.
- 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 anAnimal
. -
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 aCollar
.
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");
}
In this case:
- We use inheritance to represent that a
Cat
and aDog
are types ofAnimal
. - We use composition to indicate that a
Dog
has aCollar
.
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.
Posted on May 24, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.