Introduction to Object Oriented Programming Concepts in C#
Aug 15, 2015 02:00 0 Comments C# (C-Sharp) Asadul

Inheritance and its types in C#        

 

Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in C# by which one class is allowed to inherit the features(fields and methods) of another class.

 

Important terms in inheritance:

 

·         Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).

 

·         Sub Class: The class that inherits the other class is known as subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.

 

·         Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

 

Syntax:

 

Class derived-class: base-class

{

   // methods and fields

}

 

Example:

using System;

namespace InterfaceExample {

// Base class

class ParentClass {

   // data members

    public string name;

    public string subject;

    // public method of base class 

    public void readers(string name, string subject)

    {

        this.name = name;

        this.subject = subject;

        Console.WriteLine("My name is: " + name); 

        Console.WriteLine("My Favorite Subject is: " + subject);

    }

}

// inheriting the ParentClass class using : (colon)

class ChildClass : ParentClass {

    // constructor of derived class

    public ChildClass()

    {

        Console.WriteLine("Welcome");

    }

}

// Main class

class Program {

  

    // Main Method

    static void Main(string[] args)

    {

   

        // creating object of derived class

        ChildClass obj = new ChildClass();

  

        // calling the method of base class 

        // using the derived class object

        obj.readers("Parth", "C#");

    }

}

}

 

Output:

Welcome

My name is: Parth

My Favorite Subject is: C#

Types of Inheritance in C#

 

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

 

A

B

 
  b2ap3_thumbnail_new.png

 

 

 

 

 

 

 

2.       Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, 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.

A

B

C

 

 

 

 

 

 

 

 

 

 

 

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

A

B

C

D

 
 

 

 

 

 

 

 

 

 

 

 

4.       Multiple Inheritance(Through Interfaces):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 interface A and B.

A

B

C

 
 

 

 

 

 

 

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect