Inheritance implementation in C#

 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:

  1.  Single Inheritance. 
  2. Multilevel Inheritance.
  3.  Hierarchical Inheritance. 
  4. Multiple Inheritance (only with interfaces).


Types of inheritance in c#










Single Inheritance

In a single-level inheritance, there is one base class and one derived class, as given below:

Single-level inheritance


Here, class B is derived from class A.


Let's take an example to implement this in C#:
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();
}
}
view raw Program.cs hosted with ❤ by GitHub



Multi-level Inheritance


In a multi-level inheritance, A class is derived from another class in a chain way, as given below:

Multi-level Inheritance


Here, class B is derived from class A, and further class C is derived from class B.

Let's take an example to implement this in C#:
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.
};
}
}
view raw Program.cs hosted with ❤ by GitHub



Hierarchical Inheritance

In a hierarchical inheritance, multiple classes are derived from the same base class, as given below:

Hierarchical Inheritance




Here, both class B and C are derived from class A.


Let's take an example to implement this in C#:
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
};
}
}
view raw Program.cs hosted with ❤ by GitHub


Multiple Inheritances


In multiple inheritances, a class has multiple base classes. C# doesn't allow to inherit multiple classes, but allows to inherit multiple interfaces with or without a single base class, as given below:

Multiple Inheritances




Here, class C is derived from class A and interface IB.

C# compiler is designed not to support multiple inheritance because it causes ambiguity of methods from different base class.


Let's take an example to implement this in C#:
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"
};
}
}
view raw Program.cs hosted with ❤ by GitHub

Vikash Chauhan

C# & .NET experienced Software Engineer with a demonstrated history of working in the computer software industry.

Post a Comment

Previous Post Next Post

Contact Form