C# Inheritance (original) (raw)

Last Updated : 15 Jan, 2025

Inheritance is a fundamental concept in object-oriented programming that allows a child class to inherit the properties from the superclass. The new class inherits the properties and methods of the existing class and can also add new properties and methods of its own. Inheritance promotes code reuse, simplifies code maintenance, and improves code organization.

Syntax:

class derived-class : parent-class
{
// methods and fields
}

**Example:

C# `

// Demonstration of Inheritance using System;

// Base class class GFG { // Data members public string name; public string subject;

public void readers(string name, string subject)
{
    this.name = name;
    this.subject = subject;
    Console.WriteLine("Myself: " + name);
    Console.WriteLine("My Favorite Subject is: " + subject);
}

}

// Inheriting the GFG class class GeeksforGeeks : GFG { // Constructor of derived class public GeeksforGeeks() { Console.WriteLine("GeeksforGeeks"); } }

class Geeks { static void Main(string[] args) { // Child class object GeeksforGeeks g = new GeeksforGeeks();

    // Calling the method of base class
    // using the derived class object
    g.readers("Geek", "C#");
}

}

`

Output

GeeksforGeeks Myself: Geek My Favorite Subject is: C#

Types of Inheritance

In C#, there are mainly 4 types of inheritance which are described below:

1. Single Inheritance

In single inheritance, subclasses inherit the features of one superclass. In the image below, the class A serves as a base class for the derived class B.

2. Multilevel Inheritance

In Multilevel Inheritance, a derived class will inherit a base class and as well as the derived class also act as the base class for another class. In the below image, class A serves as a base class for the derived class B, which serves as a base class for the derived class C.

3. Hierarchical Inheritance

In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the below image, class A serves as a base class for the derived classes B, C, and D.

4. Multiple Inheritance

In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes. Please note that C# does not support multiple inheritance with classes. In C#, we can achieve multiple inheritance only through Interfaces. In the image below, Class C is derived from interfaces A and B.

**5. Hybrid Inheritance (Through Interfaces)

It is a mix of two or more of the above types of inheritance. Since C# doesn’t support multiple inheritance with classes, hybrid inheritance is also not possible with classes. In C#, we can achieve hybrid inheritance only through Interfaces.

**Example:

C# `

// Different types of Inheritance using System;

// Single Inheritance class Animal { public void Eat() { Console.WriteLine("Animal is eating."); } }

class Dog : Animal { public void Bark() { Console.WriteLine("Dog is barking."); } }

// Multi-level Inheritance class Mammal : Animal { public void Run() { Console.WriteLine("Mammal is running."); } }

class Horse : Mammal { public void Gallop() { Console.WriteLine("Horse is galloping."); } }

// Hierarchical Inheritance class Bird : Animal { public void Fly() { Console.WriteLine("Bird is flying."); } }

class Eagle : Bird { public void Hunt() { Console.WriteLine("Eagle is hunting."); } }

class Penguin : Bird { public void Swim() { Console.WriteLine("Penguin is swimming."); } }

// Multiple Inheritance interface I1 { void Method1(); }

interface I2 { void Method2(); }

class MyClass : I1, I2 { public void Method1() { Console.WriteLine("Method1 is called."); }

public void Method2()
{
    Console.WriteLine("Method2 is called.");
}

}

class Geeks { // Main Method static void Main(string[] args) { // single inheritance Dog dog = new Dog(); dog.Eat();

    // multi-level inheritance
    Horse horse = new Horse();
    horse.Eat();
    horse.Run();

    // hierarchical inheritance
    Eagle eagle = new Eagle();
    Penguin penguin = new Penguin();
    eagle.Fly();
    eagle.Hunt();
    penguin.Fly();
    penguin.Swim();

    // multiple inheritance
    MyClass myClass = new MyClass();
    myClass.Method1();
    myClass.Method2();

    Console.ReadLine();
}

}

`

Output

Animal is eating. Animal is eating. Mammal is running. Bird is flying. Eagle is hunting. Bird is flying. Penguin is swimming. Method1 is called. Method2 is called.

Important Terms

Advantages of Inheritance

Disadvantages of Inheritance