Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Inheritance vs Composition in C#
In C#, there are two fundamental ways to establish relationships between classes: inheritance and composition. Inheritance creates an "IS-A" relationship where a derived class inherits behavior from a base class, while composition creates a "HAS-A" relationship where one class contains instances of other classes as components.
Inheritance
Inheritance allows you to designate that a new class should inherit the members of an existing class. The existing class is called the base class, and the new class is referred to as the derived class. Inheritance implements the IS-A relationship. For example, mammal IS-A animal, dog IS-A mammal, hence dog IS-A animal as well.
Syntax
public class BaseClass {
// base class members
}
public class DerivedClass : BaseClass {
// derived class inherits base class members
// can add new members or override existing ones
}
Example
using System;
public class Shape {
protected string color;
public Shape(string color) {
this.color = color;
}
public virtual void Draw() {
Console.WriteLine("Drawing a shape with color: " + color);
}
}
public class Circle : Shape {
private double radius;
public Circle(string color, double radius) : base(color) {
this.radius = radius;
}
public override void Draw() {
Console.WriteLine("Drawing a circle with color: " + color + " and radius: " + radius);
}
}
public class Program {
public static void Main() {
Shape shape = new Shape("Red");
shape.Draw();
Circle circle = new Circle("Blue", 5.0);
circle.Draw();
}
}
The output of the above code is −
Drawing a shape with color: Red Drawing a circle with color: Blue and radius: 5
Composition
Composition creates a HAS-A relationship where one class contains instances of other classes as components. Under composition, if the parent object is deleted, then the child object also loses its status. Composition is a special type of aggregation and gives a part-of relationship.
Syntax
public class Component {
// component class
}
public class Container {
private Component component = new Component();
// container HAS-A component
}
Example
using System;
public class Engine {
private string type;
public Engine(string type) {
this.type = type;
}
public void Start() {
Console.WriteLine(type + " engine started");
}
public void Stop() {
Console.WriteLine(type + " engine stopped");
}
}
public class Car {
private Engine engine;
private string brand;
public Car(string brand, string engineType) {
this.brand = brand;
this.engine = new Engine(engineType);
}
public void StartCar() {
Console.WriteLine(brand + " car starting...");
engine.Start();
}
public void StopCar() {
Console.WriteLine(brand + " car stopping...");
engine.Stop();
}
}
public class Program {
public static void Main() {
Car car = new Car("Toyota", "V6");
car.StartCar();
car.StopCar();
}
}
The output of the above code is −
Toyota car starting... V6 engine started Toyota car stopping... V6 engine stopped
Inheritance vs Composition Comparison
| Aspect | Inheritance | Composition |
|---|---|---|
| Relationship | IS-A relationship | HAS-A relationship |
| Coupling | Tight coupling between base and derived class | Loose coupling between classes |
| Flexibility | Less flexible - compile-time binding | More flexible - runtime composition |
| Code Reuse | Reuses code through inheritance | Reuses code through object delegation |
| Memory | Single object in memory | Multiple objects in memory |
When to Use Each Approach
Use Inheritance when:
-
You have a clear IS-A relationship
-
You want to override or extend specific behaviors
-
The relationship is stable and unlikely to change
Use Composition when:
-
You have a HAS-A relationship
-
You need more flexibility and loose coupling
-
You want to change behavior at runtime
Conclusion
Both inheritance and composition are important object-oriented design patterns in C#. Inheritance creates IS-A relationships with tighter coupling, while composition creates HAS-A relationships with greater flexibility. Choose inheritance for clear hierarchical relationships and composition for building complex objects from simpler components.
