Inheritance in C#

What is inheritance?

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 such as superclass or base class and then forming them into a hierarchy of classes. Also, inheritance is the primary pillar of the OOPS concept.

Types of inheritance

  • Single inheritance
  • Multilevel inheritance
  • Hierarchical inheritance

Example of single inheritance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace single_inheritance
{
    class A  //this is A class
    {
        public void show()
        {
            Console.WriteLine("This is A class");
        }
    }
    class Program : A  //colon sign(:) are used into inherit parent class to derived class
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.show();   //calling A class show() function
            Console.ReadLine();
        }
    }
}

Output of single inheritance

This is A class

Example of multi-level inheritance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace single_inheritance
{
    class A  //this is A class
    {
        public void showA()
        {
            Console.WriteLine("This is A class");
        }
    }
    class B : A    // B class is inherited from A class
    {
        public void showB()
        {
            Console.WriteLine("This is B class");
        }
    }
    class Program : B  // Program class is inherited from B class
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.showA();  //calling A class showA() function
            p.showB();  //calling B class showB() function
            Console.ReadLine();
        }
    }
}

Output of multi-level inheritance

This is A class
This is B class

Example of hierarchical inheritance

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace single_inheritance
{
    class A  //this is A class
    {
        public void showA()
        {
            Console.WriteLine("This is A class");
        }
    }
    class B : A  // B class is inherited from A class
    {
        public void showB()
        {
            Console.WriteLine("This is B class");
        }
    }
    class C : A  // C class is inherited from A class
    {
        public void showC()
        {
            Console.WriteLine("This is C class");
        }
    }
    class Program  
    {
        static void Main(string[] args)
        {
            B objB = new B();
            objB.showA();  //calling the method of parent class showA() method
            objB.showB();  //as well as calling the own class method

            C objC = new C();
            objC.showA();  //calling the method of parent class showA() method
            objC.showC();  //as well as calling the own class method

            Console.ReadLine();
        }
    }
}

Output of hierarchical inheritance

This is A class This is B class This is A class This is C class

Note: c# does not support multiple inheritance but support the multiple interface inheritance.



Write a comment


Name Email Message