In object-oriented programming, Inheritance is one of the most important concepts because it allows the creation of hierarchical classifications. It is the mechanism by which one class is allowed to inherit the features(fields, properties, indexers, and methods) of another class. It provides the ability to reuse the same code, instead of writing the same code again and again.
In C#, a class that is inherited is called a base class. The class that does the inheriting is named a derived class. Therefore, a derived class is a specialized version of a base class. It inherits all of the fields, methods, properties, and indexers defined by the base class (except private members )and adds its own unique elements.
Inheritance supports the concept of reusability. When we want to create a new class and there is already a class that includes some common code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields, methods, properties, and indexers of the existing class.
In C#, we can inherit a class with the help of the colon operator (:).
C# supports the following four types of inheritance:
- Single Inheritance.
- Multilevel Inheritance.
- Hierarchical Inheritance.
- Multiple Inheritance (only with interfaces).
Single Inheritance
namespace Inheritance; | |
public class Animal | |
{ | |
public bool Walk { get; set; } | |
public bool Eat { get; set; } | |
} | |
public class Dog: Animal | |
{ | |
public string Name { get; set; } | |
public void Speak() | |
{ | |
Console.WriteLine("Barking"); | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var myDog = new Dog() | |
{ | |
Name = "Jack", | |
Walk = true, // Inherited from Animal base class. | |
Eat = true // Inherited from Animal base class. | |
}; | |
myDog.Speak(); | |
} | |
} |
Multi-level Inheritance
namespace Inheritance; | |
public class Vehicle | |
{ | |
public bool CanRun { get; set; } | |
} | |
public class Car: Vehicle | |
{ | |
public bool HasEngine { get; set; } | |
public void VehicleType() | |
{ | |
Console.WriteLine("Car"); | |
} | |
} | |
public class Maruti : Car | |
{ | |
public string Model { get; set; } | |
public void Brand() | |
{ | |
Console.WriteLine("Maruti"); | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var myCar = new Maruti() | |
{ | |
Model = "800", | |
CanRun = true, // Inherited from Vehicle base class. | |
HasEngine = true // Inherited from Car base class. | |
}; | |
} | |
} |
Hierarchical Inheritance
namespace Inheritance; | |
public class Vehicle | |
{ | |
public bool CanRun { get; set; } | |
} | |
public class Car: Vehicle | |
{ | |
public string Model { get; set; } | |
public bool HasEngine { get; set; } | |
public int Wheels { get; set; } | |
public void VehicleType() | |
{ | |
Console.WriteLine("Car"); | |
} | |
} | |
public class Bike : Vehicle | |
{ | |
public string Model { get; set; } | |
public bool HasEngine { get; set; } | |
public int Wheels { get; set; } | |
public void VehicleType() | |
{ | |
Console.WriteLine("Bike"); | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var myCar = new Car() | |
{ | |
Model = "Maruti 800", | |
CanRun = true, // Inherited from Vehicle base class. | |
HasEngine = true, | |
Wheels = 4 | |
}; | |
var myBike = new Bike() | |
{ | |
Model = "Yamaha rx100", | |
CanRun = true, // Inherited from Vehicle base class. | |
HasEngine = true, | |
Wheels = 2 | |
}; | |
} | |
} |
Multiple Inheritances
namespace Inheritance; | |
public interface IVehicle | |
{ | |
public void VehicleType(); | |
} | |
public class Vehicle | |
{ | |
public bool CanRun { get; set; } | |
public string Model { get; set; } | |
public bool HasEngine { get; set; } | |
public int Wheels { get; set; } | |
} | |
public class Car : Vehicle, IVehicle | |
{ | |
public string Brand { get; set; } | |
public void VehicleType() | |
{ | |
Console.WriteLine("Car"); | |
} | |
} | |
public class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
var myMaruti = new Car() | |
{ | |
Model = "Maruti 800", // Inherited from Vehicle base class. | |
CanRun = true, // Inherited from Vehicle base class. | |
HasEngine = true, // Inherited from Vehicle base class. | |
Wheels = 4, // Inherited from Vehicle base class. | |
Brand = "Maruti" | |
}; | |
} | |
} |