Skip to main content

Command Palette

Search for a command to run...

Inheritance In C#

Published
5 min read

What is Inheritance?

Inheritance is one of the core concepts of the OOPS.The definition of Inheritance is one class deriving the property of another class is known as inheritance.

  • The class from which we are going to inherit the properties then that class is known as superclass or base class or parent class.

  • The class which is inheriting the property of the class is known as the subclass or derived class or child class.

class Subclass : SuperClass
    {
        //methods and fields
    }

Note: In C# we use : to inherit other classes.

Why do we need inheritance?

  • Using inheritance we can reuse the code instead of rewriting the code again and again.

  • Using inheritance the number of lines of code can be reduced.

Types of inheritance :

These are the types of inheritance.

  1. Single Inheritance.
  2. Multilevel Inheritance.
  3. Multiple Inheritance.
  4. Hierarchical Inheritance.
  5. Hybrid Inheritance.

1. Single Inheritance :

  • One class inheriting the properties of another class is called as Single inheritance.
class A
    {
        public void display()
        {
            Console.WriteLine("display() of Superclass");
        }
    }

    class B : A
    {
        public void view()
        {
            Console.WriteLine("view() of ChildClass");
        }
    }
    class Program
    {
        public static void Main()
        {
            B b = new B();
            //Using this the property of the superclass can also be called
            b.view(); //Sub class method
            b.display(); //Super class method
        }
    }
/*
Output :
view() of ChildClass
display() of Superclass
*/

2. Multilevel Inheritance :

In this type of inheritance subclass inheritance, the properties of the superclass and this superclass inherit the properties of another superclass is known as the Multilevel Inheritance.

class A
    {
        public void display()
        {
            Console.WriteLine("display() of class of A");
        }
    }

    class B : A
    {
        public void view()
        {
            Console.WriteLine("view() of class of B");
        }
    }
    class C : B
    {
        public void run()
        {
            Console.WriteLine("run() of class of C");
        }
    }
    class Program
    {
        public static void Main()
        {
            C c = new C();
            c.run();
            c.view();
            c.display();
        }
    }
*/
Output :
run() of class of C
view() of class of B
display() of class of A
*/

Note: We can access the properties of all the classes using the Object of class C because C is inheriting the class B and class B is inheriting the class A.

3. Multiple Inheritance :

In this type of inheritance one, subclass inheriting the properties of the multiple classes is known as Multiple Inheritance.

Note: The multiple inheritance is not supported in C#.

4. Hierarchical Inheritance :

In this type of inheritance from one superclass, multiple subclasses inheritances the properties this creates the hierarchical structure so, this is called as the Hierarchical Inheritance.

class Car
    {
        public void drive()
        {
            Console.WriteLine("drive() of the Car class");
        }
    }

    class Tesla : Car
    {
        public void electric()
        {
            Console.WriteLine("electric() of Tesla");
        }
    }
    class RollsRoyce : Car 
    {
        public void luxury()
        {
            Console.WriteLine("luxury() of Rolls-Royce");
        }
    }
    class Program
    {
        public static void Main()
        {
            Tesla tesla = new Tesla();

            tesla.drive();
            tesla.electric();

            RollsRoyce rolls = new RollsRoyce();

            rolls.drive();
            rolls.luxury();
        }
    }
/*
Output :
drive() of the Car class
electric() of Tesla
drive() of the Car class
luxury() of Rolls-Royce
*/

Note: In both the subclasses the basic property that is drive() is inherited along with their own methods.

5. Hybrid Inheritance :

Hybrid Inheritance is the combination of the other type of inheritance.

More from this blog

Untitled Publication

14 posts

React Developer | Frontend Developer