Inheritance in C# is an object-oriented programming (OOP) feature that allows one class to derive properties and behaviors from another class. It promotes code reusability, extensibility and establishes a natural hierarchical relationship between classes.
Example: Basic Inheritance
C#
using System;
class Animal {
public void Eat() {
Console.WriteLine("This animal eats food.");
}
}
class Dog : Animal {
public void Bark() {
Console.WriteLine("The dog barks.");
}
}
class Program {
static void Main() {
Dog d = new Dog();
d.Eat(); // Inherited method
d.Bark(); // Derived class method
}
}
OutputThis animal eats food.
The dog barks.
Syntax
class BaseClass {
// Members of base class
}
class DerivedClass : BaseClass {
// Members of derived class
}
- Inheritance is achieved using the : (colon) symbol.
- Derived Class: The class that inherits the other class is known as asubclass(or a derived class, extended class or child class).
- Parent Class: The class whose features are inherited is known as a superclass (or a base class or a parent class).
- C# supports single inheritance (a class can directly inherit from only one class).
- A derived class can itself serve as a base for another class.
- All classes in C# implicitly inherit from the System.Object class.
Types of Inheritance
C# directly supports the following inheritance forms:
- Single Inheritance: One class derives from one base class.
- Multilevel Inheritance: A class derives from another derived class.
- Hierarchical Inheritance: Multiple classes derive from a single base class.
- Multiple Inheritance (Through Interfaces): A class can implement multiple interfaces, achieving multiple inheritance indirectly, since C# does not allow multiple base classes.
1. Single Inheritance
In single inheritance, subclasses inherit the features of one superclass.
Single InheritanceIn the above image, the class A serves as a base class for the derived class B.
2. Multilevel Inheritance
In Multilevel Inheritance, a derived class will inherit a base class and as well as the derived class also act as the base class for another class.
Multilevel InheritanceIn the above image, class A serves as a base class for the derived class B, which serves as a base class for the derived class C.
3. Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass.
In the above image, X and Y are sub-class (child class) that inherits property from class B and M and N are sub-class (child class) that inherits property from class C. B is the parent class of X and Y and C is the parent class of M and N.
4. Multiple Inheritance
In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes.
Please note that C# does not support multiple inheritance with classes. In C#, we can achieve multiple inheritance only through Interfaces.
In the image above, Class C is derived from interfaces A and B.
Advantages of Inheritance
- Code Reusability: Inheritance allows us to reuse existing code by inheriting properties and methods from an existing class.
- Code Maintenance: Inheritance makes code maintenance easier by allowing us to modify the base class and have the changes automatically reflected in the derived classes.
- Code Organization: Inheritance improves code organization by grouping related classes together in a hierarchical structure.
Disadvantages of Inheritance
- Tight Coupling: Inheritance creates a tight coupling between the base class and the derived class, which can make the code more difficult to maintain.
- Complexity: Inheritance can increase the complexity of the code by introducing additional levels of abstraction.
- Fragility: Inheritance can make the code more fragile by creating dependencies between the base class and the derived class.
Explore
Introduction
Fundamentals
Control Statements
OOP Concepts
Methods
Arrays
ArrayList
String
Tuple
Indexers