Inheritance in C# | C# Tutorials
Inheritance in C# | C# Tutorials
Inheritance is pretty much a crucial concept for all the OOP languages and it is so for C Sharp. Inheritance allows us to define a class baes on another class. Basically inheriting features in one class from another.
This makes creating and maintaining an application easy in such a way that we can reuse, extend, or modify the attributes and behaviours of the defined class.
In C# the class from which the properties and attributes are inherited by another class is called the base class and the one to which all the properties and attributes are inherited is called the derived class.
There are some important things about inheritance
Super Class: This is the same as the base class. Basically, where the attributes and properties are to be inherited.
Sub Class: This is the same as the derived class. The class which inherits the properties and attributes from the superclass or base class. The subclass can add its own fields and methods in addition to the superclass fields and methods.
Reusability: The concept of reusability is highly supported by inheritance. When we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class.
There are a few types of inheritance in C Sharp.
- Single inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Multiple inheritances
- Hybrid inheritance
Single Inheritance
When one class inherits another it is known as single inheritance. For instance class A inheriting all the properties and attributes from class B only.
Example:
using System;
public class Students
{
public float attendance = 300;
}
public class Coder: Students
{
public float extraCredit = 100;
}
class TestInheritance{
public static void Main(string[] args)
{
Coder p1 = new Coder();
Console.WriteLine("Attendance: " + p1. attendance);
Console.WriteLine("Credit: " + p1. extraCredit);
}
}
Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. This means that class C will inherit from class B and class B will inherit from class A.
Example:
using System;
public class Animal
{
public void eat() { Console.WriteLine("Eating..."); }
}
public class Dog: Animal
{
public void bark() { Console.WriteLine("Barking..."); }
}
public class BabyDog : Dog
{
public void weep() { Console.WriteLine("Weeping..."); }
}
class TestInheritance2{
public static void Main(string[] args)
{
BabyDog d1 = new BabyDog();
d1.eat();
d1.bark();
d1.weep();
}
}
Hierarchical Inheritance
In this type of inheritance one class acts like a superclass or base class from which other classes inherit the properties and attributes. For instance, classes A, B, and C inheriting all the stuff from class X.
Multiple Inheritance
This is a kind of hierarchical inheritance but it is reversed. This means that one class will inherit from more than one class or you can say one class will have more than one base class to inherit all the properties and attributes. For example, class A inheriting from both class X and class Y.
NOTE: C Sharp does not allow multiple inheritances with classes, we can only do it with the help of interfaces.
Hybrid Inheritance
It is a mix of two or more of the above types of inheritance. Since C# doesn’t support multiple inheritances with classes, hybrid inheritance is also not possible with classes. In C#, we can achieve hybrid inheritance only through Interfaces.