Click here to Skip to main content
15,888,461 members
Articles / Programming Languages / C#

Inheritance in OOP C#

Rate me:
Please Sign up or sign in to vote.
4.11/5 (6 votes)
19 Oct 2015CPOL3 min read 29.1K   4   4
Inheritance in OOP C#

Introduction

Previously, we have discussed about the Abstraction and Encapsulation and their difference in this blog. Today, we will go for the other two properties of OOP in C#, these are Inheritance and Polymorphism.

Description

As we all know, OOPs has 4 properties like:

  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism

Already we have discussed about the first two, abstraction & encapsulation. Let's discuss about the other two, Inheritance and Polymorphism.

Inheritance

Inheritance means getting some thing (properties) as heredity. To get that, we need a hierarchical structure. And that is provided by OOP using Inheritance. Here in inheritance, we have a concept of base class and sub class. The base class is also known as parent class, super class, etc. and the sub class is also known as child class. The properties (variables) and methods are accessible to the sub class, but keep in mind that only the public ones. The private methods or variables are not accessible from the child class. And the methods are accessible can be called without creating the object of that super class. 

To inherit a class from another class, you have to extend the sub class to super class. Follow the example given below:

C#
class ClassSuper
{
    public void MethdoTestPublic()
    {
        Console.WriteLine("ClassTest.MethdoTestPublic");
    }

    private void MethdoTestPrivate()
    {
        Console.WriteLine("ClassTest.MethdoTestPublic");
    }
}
    
class ClassSub : ClassSuper // Inheriting from ClassSuper
{
    public void MethodTest()
    {
        MethdoTestPublic(); // calling the super class method
        MethdoTestPrivate(); // calling the super class private method [Error]
        Console.WriteLine("ClassTest2.MethodTest");
    }
}

Here, ClassSub is extending the ClassSuper to get access to the methods and properties of the super class.

Type of Inheritance:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multi Level Inheritance
  4. Hierarchical Inheritance

Single Inheritance

The above example is one of the examples of Single Inheritance. 

Image 1

C#
class ClassSuper
{
    // Do your code
}
    
class ClassSub : ClassSuper // Inheriting from ClassSuper
{
    // Do your code
}

Multi Level Inheritance

Multi level inheritance is the joining of two or more single inheritance. Like Sub Class 2 is inheriting Sub Class 1 and Sub Class 1 is inheriting Super Class. If you watch closely, there are two single inheritances present. One is Sub Class 1 -> Sub Class 2 and second one is Sub Class 1 -> Super Class.

Image 2

C#
class ClassSuper
{
    // Do your code
}
    
class ClassSub1 : ClassSuper // Inheriting from ClassSuper
{
    // Do your code
}
class ClassSub2 : ClassSub1 // Inheriting from ClassSub2
{
    // Do your code
}

Hierarchical Inheritance

In case of Hierarchical  interface, we have multiple sub classes which are inheriting one super class. Like here Sub Class 1 and Sub Class 2, both are inheriting Super Class. This is Hierarchical Inheritance.

Image 3

C#
class ClassSuper
{
    // Do your code
}
    
class ClassSub1 : ClassSuper // Inheriting from ClassSuper
{
    // Do your code
}
class ClassSub2 : ClassSuper // Inheriting from ClassSuper
{
    // Do your code
}

Multiple Inheritance

Multiple Inheritance is just the opposite of Hierarchical Inheritance. Here one sub class is inheriting multiple Super Classes. Like any other Object Oriented Programming languages, C# also doesn't support Multiple Inheritance. To achieve that, we have to go for Interface, which we will discuss later.

Image 4

Like here, the only Sub Class is inheriting the two Super Classes (Super Class 1 and Super Class 2). This architecture is not possible with C# classes. So we are taking help of Interface to do so.

C#
interface ISuper1
{
    // Do your code
}
    
interface ISuper2
{
    // Do your code
}
class ClassSub : ISuper1, ISuper2 // Inheriting from ISuper1 and ISuper2
{
    // Do your code
}

Advantages of Inheritance

  1. Reusability: Use the base class public methods and properties from the base class, without defining or creating any instance.
  2. Extensibility: Extend the base class methods logic in the sub class without rewriting the same.
  3. Data hiding: Base class can keep some data private so that it cannot be altered by the derived class.
  4. Override: Base class methods can be overridden by sub class methods to reuse in a meaningful way.

Disadvantages of Inheritance

  1. Tightly Coupled: In inheritance, both the classes (sub and super class) are tightly coupled. So if any change occurs in super class, it will affect in the base class. 
  2. Sometimes, a lot of data remains unused in the hierarchy. So memory wastage can happen in case of inheritance.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer PwC
India India
I am a Software developer having an experience of 5 years in application development. To get me you can mail me at arkadeepde@gmail.com or you can visit my blog at ASP With Arka

Comments and Discussions

 
SuggestionUnclear and incomplete Pin
Alexandru Lungu26-Oct-15 9:12
professionalAlexandru Lungu26-Oct-15 9:12 
GeneralRe: Unclear and incomplete Pin
Arkadeep De27-Oct-15 0:21
professionalArkadeep De27-Oct-15 0:21 
QuestionMissing explanations about constructors involved in inheritance Pin
Alexandru Lungu26-Oct-15 2:42
professionalAlexandru Lungu26-Oct-15 2:42 
- How does the constructors get called
- How about constructors with parameters that differ partially or entirely from the parent class

These are aspects that need addressing.

General"Data Hiding" is not an advantage of inheritance... Pin
Alexandru Lungu26-Oct-15 2:32
professionalAlexandru Lungu26-Oct-15 2:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.