In this article you will learn about the Inheritance along with types, advantage and disadvantages.

Inheritance: It is an important pillar of Object Oriented Programming. The ability of creating a new class from an existing class. Inheritance is when an object acquires the property of another object. Inheritance allows a subclass to acquire the properties and behavior of the superclass. It helps to reuse, customize and enhance the existing code. So it helps to write a code accurately and reduce the development time.

Inheritance is a process in which one object acquires all the properties and behaviors of its parent object automatically. In such way, you can reuse, extend or modify the attributes and behaviors which is defined in a superclass or another class.

In C#, the class which inherits the members of another class is called derived class and the class whose members are inherited is called base class. The derived class is the specialized class for the base class.

As per Wikipedia, In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object or class, retaining similar implementation. Also defined as deriving new classes from existing ones and forming them into a hierarchy of classes.

Important terminology

· Super Class: The class whose features is inherited is known as super/base/parent class.

· Sub Class: The class that inherits the other class is known as sub/derived/extended/child class.

· Reusability: Inheritance supports the concept of “reusability”, i.e. the reuse of properties of the base class in the derived classes.

class Derived-class-name : Base-class -name {

// methods and fields

.

}

Example:

  1. using System;

    namespace InheritanceDemo

    {

    class BaseClass

    {

    public string Name { get; set; }

    public string Subject { get; set; }

    public void Display(string name, string subject)

    {

    Name = name;

    Subject = subject;

    Console.WriteLine($"Profession: {name}\nFavorite Subject is: {subject}");

    }

    }

    class Derviedlass : BaseClass

    {

    public Derviedlass()

    {

    Console.WriteLine("Inheritance Demo");

    }

    }

    class Demo

    {

    private static void Main()

    {

    Derviedlass derviedObject = new Derviedlass();

    derviedObject.Display("Software Developer", "C#");

    }

    }

    }

Note: In the above example of inheritance, class BaseClass is a base class, class Derviedlass is a derived class which extends BaseClass class and class Demo is a driver class to run program.

Output:

Types of Inheritance in C#

Below are the different types of inheritance which is supported by C# in different combinations.

  1. Single
  2. Multilevel
  3. Hierarchical
  4. Multiple
  5. Hybrid
1. Single Inheritance: In single inheritance, derived classes inherit the features of one base class.
Note: Let’s understand it by real life example, Son (B) class inherits Father (A) class.
In the below syntax, the class A serves as a base class for the derived class B.

Syntax:

class A {

}

class B: A {

}

Example:

  1. using System;

    namespace SingleInheritanceDemo

    {

    class Demo

    {

    class Father

    {

    public void Display()

    {

    Console.WriteLine("Display");

    }

    }

    class Son : Father

    {

    public void DisplayOne()

    {

    Console.WriteLine("DisplayOne");

    }

    }

    static void Main()

    {

    Father f = new Father();

    f.Display();

    Son s = new Son();

    s.Display();

    s.DisplayOne();

    Console.ReadKey();

    }

    }

    }

Output:

2. Multilevel Inheritance: When multiple classes are involved and their parent-child relation is formed in a chained way, then such formation is known as multi-level inheritance.

Note: Let’s understand it by real life example, Son (C) class inherits Father (B) class, Father Class inherits Grand Father (A) class.

In the below syntax, class a serves as a base class for the derived class B, which in turn serves as a base class for the derived class C.

Syntax:

class A {

}

class B: A {

}

class C: B {

}

Example:

  1. using System;

    namespace MultilevelInheritanceDemo

    {

    class Grandfather

    {

    public void Display()

    {

    Console.WriteLine("Grandfather");

    }

    }

    class Father : Grandfather

    {

    public void DisplayOne()

    {

    Console.WriteLine("Father");

    }

    }

    class Son : Father

    {

    public void DisplayTwo()

    {

    Console.WriteLine("Son");

    }

    static void Main()

    {

    Son s = new Son();

    s.Display();

    s.DisplayOne();

    s.DisplayTwo();

    Console.Read();

    }

    }

    }

Output:

3. Hierarchical Inheritance: In Hierarchical Inheritance, more than one classes inherit a same class.

Note: Let’s understand it by real life example, Son (B) and Daughter (C) both inherit Father (A) class.

In the below syntax, class A serves as a base class for the derived class B and C.

Syntax:

class A {

}

class B: A {

}

class C: A {

}

Example :

  1. using System;

    namespace InheritanceDemo

    {

    class Program

    {

    class Principal

    {

    public void Monitor()

    {

    Console.WriteLine("Monitor");

    }

    }

    class Teacher : Principal

    {

    public void Teach()

    {

    Console.WriteLine("Teach");

    }

    }

    class Student : Principal

    {

    public void Learn()

    {

    Console.WriteLine("Learn");

    }

    }

    static void Main(string[] args)

    {

    Principal g = new Principal();

    g.Monitor();

    Teacher d = new Teacher();

    d.Monitor();

    d.Teach();

    Student s = new Student();

    s.Monitor();

    s.Learn();

    Console.ReadKey();

    }

    }

    }

Output:

4. Multiple Inheritance: In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes.

Note:

  • Let’s understand it by real life example, Son (B) and Daughter (C) both inherit Father (A) belongings (class).
  • C# does not support multiple inheritance with classes. In C#, we can achieve multiple inheritance only through Interfaces.

In the below syntax, Class C is derived from interface A and B.

Syntax:

interface A {

}

interface B{

}

class C: A,B {

}

Example:

  1. using System;

    using System.Collections;

    namespace InheritanceDemo

    {

    interface IInterface1

    {

    void Languages();

    }

    class Class1 : IInterface1

    {

    public void Languages()

    {

    var languages = new ArrayList

    {

    "C#",

    "Java"

    };

    Console.WriteLine("Languages provided by Udemy:");

    foreach (var language in languages)

    {

    Console.WriteLine(language);

    }

    }

    }

    interface IInterface2

    {

    void Courses();

    }

    class Class2 : IInterface2

    {

    public void Courses()

    {

    var cources = new ArrayList

    {

    "Lets code C# : Candy Crush in Unity",

    "Naked C#: A Beginner's Guide to Coding",

    "Complete C# Masterclass"

    };

    Console.WriteLine("\nCourses provided by Udemy:");

    foreach (var cource in cources)

    {

    Console.WriteLine(cource);

    }

    }

    }

    class MIDemoClass : IInterface1, IInterface2

    {

    readonly Class1 obj1 = new Class1();

    readonly Class2 obj2 = new Class2();

    public void Languages()

    {

    obj1.Languages();

    }

    public void Courses()

    {

    obj2.Courses();

    }

    }

    public class DemoClass

    {

    static public void Main()

    {

    MIDemoClass obj = new MIDemoClass();

    obj.Languages();

    obj.Courses();

    }

    }

    }

Output:

5. Hybrid Inheritance: Combination of more than one type of inheritance. In C#, we can achieve hybrid inheritance with multiple inheritance only through Interfaces.

Note: Any combination of single, hierarchical, multiple and multilevel inheritances is called as hybrid inheritance.

In the below syntax, class A serves as a base class for the derived B, derived B is base class for the derived class C and D.

Syntax: (single and hierarchical)

class A {

}

class B:A{

}

class C: B {

}

class D: B {

}

Example:

  1. using System;

    namespace InheritanceDemo

    {

    class GradFater

    {

    public void Land()

    {

    Console.WriteLine("GradFater's land");

    }

    }

    class Father : GradFater

    {

    public void Home()

    {

    Console.WriteLine("Father's home");

    }

    public void Car()

    {

    Console.WriteLine("Father's Car");

    }

    }

    class Son : Father

    {

    public Son()

    {

    Console.WriteLine("Son...");

    }

    public void Mobile()

    {

    Console.WriteLine("Son's mobile");

    }

    }

    class Daughter : Father

    {

    public Daughter()

    {

    Console.WriteLine("Daughter...");

    }

    public void Purse()

    {

    Console.WriteLine("Daughter's purse");

    }

    }

    public class TestHybridInheritance

    {

    public static void Main()

    {

    Son s = new Son();

    s.Land();

    s.Car();

    s.Home();

    s.Mobile();

    Daughter d = new Daughter();

    d.Land();

    d.Car();

    d.Home();

    d.Purse();

    }

    }

    }

In below syntax, interface A serves as a base interface for the derived B and derived C, derived B and C is base interface for the derived class D.

Syntax: (hierarchical and multiple)

interface A {

}

interface B:A{

}

interface C: A {

}

class D: B,C {

}

Output:

Advantages

  • Reusability: The facility to use public methods of a base class without rewriting the same.
  • Extensibility: Extending the parent class logic as per the business logic of the derived class.
  • Data hiding: The base class can decide to keep some data private so that it cannot be modified in the derived class.
  • Overriding: We will be able to override the methods of the base class in the derived class.

Disadvantages

  • Tightly coupled: Base class and derived class are dependent on each other. I.e. one cannot be used independently of each other.
  • Complicated: When we need to add new features, then we have to make changes in both classes. I.e. supperclass and subclass. Inappropriate use of inheritance makes programs more complicated.
  • Overhead: Invoking member functions using objects create more compiler overheads.
  • Memory wastage: In class hierarchy, various data elements remain unused, and the memory allocated to them is not utilized.

Important Facts

· Default Superclass: Every class has one and only one direct superclass, Excluding Object class because Object class has no superclass.

· Superclass can only be one: Subclass can have only one super/base class, because multiple inheritance is not supported in C#, Although with interfaces we can achieve the multiple inheritance.

So, finally in this article we saw that what is inheritance and why we should use inheritance in C#, Also we went through about types of inheritance along with advantages and disadvantages.