C# Inheritance

What is Inheritance?

It's a mechanism of consuming the members of one class in another class by establishing a parent/child relationship between the classes, which provides reusability. I you want to learn about Inheritance in C#, you can go through a detailed article on C#Corner here- Inheritance with examples in C#.

Syntax

[modifiers] class [class-name]:[class-name]

Example

class X
    {
        public string Name=string.Empty;
        protected void Test1()
        {
            Console.WriteLine("Method Test1 from parent class.");
        }

    }
    class Y : X
    {
        static void Main()
        {
            Y y = new Y();
            y.Test1();
            y.Name = "test";
        }
    }

As per the above example, We can also call.

  • X = > Parent, Base, Super Class
  • Y => Child, Derived, Sub Class

Key Points

  1. In Inheritance, the child class can consume members of its parent class if it is the Owner of those members.
  2. In the Inheritance child class can not consume private members of its parent class.
  3. Parent classes constructor must be accessible to the child class. Otherwise, Inheritance is not possible.
  4. In Inheritance, the child class can access parent class members, but parent classes can never access any child class members.
  5. In C#, we don't support multiple Inheritance through classes. But we can achieve this using Interface
  6. The default access modifier of class members in C# is private.
  7. The default access modifier of a C# class is internal.

Types Of Inheritance in C#

In C#, there are four types of Inheritance. Read more about types of Inheritance in C# here on C#Corner- Types of Inheritance In C#.

Single Inheritance in C#

C# class can only inherit from one base class is called single Inheritance.

Example

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

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

// Example usage
class Program 
{
    static void Main(string[] args) 
    {
        // Create an instance of the Dog class
        Dog myDog = new Dog();

        // Call methods from the Dog class
        myDog.Bark();

        // Call inherited method from the Animal class
        myDog.Eat();
    }
}

MultiLevel Inheritance in C#

C# class can inherit from a derived class derived from another parent class.

Example

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

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

// Derived class
class Bulldog : Dog 
{
    public void Guard() 
    {
        Console.WriteLine("Bulldog is guarding.");
    }
}

// Example usage
class Program 
{
    static void Main(string[] args) 
    {
        // Create an instance of the Bulldog class
        Bulldog myBulldog = new Bulldog();

        // Call methods from the Bulldog class
        myBulldog.Bark();
        myBulldog.Guard();

        // Call inherited method from the Dog class
        myBulldog.Eat();
    }
}

Hierarchical Inheritance in C#

C# class can have multiple child classes inherited from the same parent class.

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

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

// Derived class
class Cat : Animal 
{
    public void Meow() 
    {
        Console.WriteLine("Cat is meowing.");
    }
}


class Program 
{
    static void Main(string[] args) 
    {
       
        Dog myDog = new Dog();

        Cat myCat = new Cat();

        myDog.Bark();

       
        myCat.Meow();
        myDog.Eat();
        myCat.Eat();
    }
}

Multiple Inheritance in C#

C# does not support multiple Inheritance through classes; a class cannot inherit from more than one base class at the same time.

Note. C# does support multiple inheritances through interfaces.

Example

interface IShape 
{
    void Draw();
}

interface IColor 
{
    void SetColor(string color);
}

class Square : IShape, IColor 
{
    private string color;

    public void Draw() 
    {
        Console.WriteLine("Drawing square.");
    }

    public void SetColor(string color) 
    {
        this.color = color;
    }

    public void PrintColor() 
    {
        Console.WriteLine("Color of square is: " + color);
    }
}

class Program 
{
    static void Main(string[] args) 
    {
        Square square = new Square();
        square.Draw();
        square.SetColor("Blue");
        square.PrintColor();
    }
}


Similar Articles