Inheritance in C#




  • What is Inheritance?


  • Inheritance is one of the most important feature of the object oriented Programming Language. In Inheritance we can reuse the properties of one class into another class by extending the class. The main idea behind Inheritance is the re usability of the programming code.
  • Types of Inheritance


  • There are two Concepts in Inheritance. Base Class and Derived class. Base class is the class from which the properties are going to be inherited. Derived class is the class in which properties are inherited.
    1. Single Inheritance
    2. In single Inheritance , There is one base class and one derived class. The properties and methods are in base class and base class is extended to derived class. For better understanding , see the below image.
      using System;
      namespace ConsoleApplication2
      {
          public class Base
          {
           public void HelloBase()
           {
           Console.WriteLine("Base Hello");
           }
          }
          public class Derived : Base
          {
           public void HelloDerived()
           {
           HelloBase();
           Console.WriteLine("Derived Hello");
           Console.ReadLine();
           }
          }
           class Program
          {
           static void Main(string[] args)
           {
            Base b = new Base();
            Derived d = new Derived();
            d.HelloBase();
            d.HelloDerived();
           }
          }
      }


      Output

      Base Hello
      Base Hello
      Derived Hello


    3. Hirarchical inheritance
    4. In this type of inheritance ,multiple classes are inherited using a single base class.The idea behind this is to use the features of a single class into multiple derived classes.
      using System;
      namespace hirarchical
      {
          public class Base
          {
           public void message()
           {
            Console.WriteLine("Base Hello");
           }
          }
          public class Derived:Base
          {
           public void DerivedMessage()
           {
            message();
            Console.WriteLine("Derived Hello");
           }
          }
          public class B : Base
          {
           public void BMessage()
           {
            message();
            Console.WriteLine("3rd derived from Base");
           }
          }
          class Program
          {
           static void Main(string[] args)
           {
            Base b = new Base();
            Derived d = new Derived();
            B bob=new B();
            bob.BMessage();
            Console.ReadKey();
           }
          }
      }


      Output

      Base Hello
      3rd Derived from Base


    5. Multilevel Inheritance
    6. In this inheritance ,The class is derived from another derived class. This type of inheritance is used when we want to use some properties of our predecessor class not the base class like hirarchical inheritance.
      using System;
      namespace Multilevel
      {
          public class Base
          {
           public void Say()
           {
            Console.WriteLine("Base Hello");
           }
          }
          public class Derived : Base
          {
           public void DerivedSay()
           {
            Say();
            Console.WriteLine("Derived Hello");
           }
          }
          public class B : Derived
          {
           public void Speak()
           {
            DerivedSay();
            Console.WriteLine("B class Hello");
           }
          }
          class Program
          {
           static void Main(string[] args)
           {
            B b = new B();
            b.Speak();
            Console.ReadLine();
           }
          }
      }


      Output
      Base Hello
      Derived Hello
      B class Hello


    7. Multiple Inheritance(Using Interface)
    8. C# does not support multiple Inheritance just like other object oriented languages. To implement multiple inheritance , c# introduced concept of Interface. Using Interface, we can implement multiple inheritance.
      using System;
      namespace interfaceexample
      {
           public interface i
           {
            string show();
           }
           public class A:i
           {
            public string show()
            {
             return "language expert";
            }
           }
      }
    Inheritance in C# Inheritance in C# Reviewed by LanguageExpert on March 02, 2018 Rating: 5

    No comments