Tuesday, October 20, 2015

Inheritance in OOPs 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 OOPs 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. Lets discuss about the other two, Inheritance and Polymorphism.

Inheritance:
Inheritance means getting some thing (properties) as heredity. To get that we need an hierarchical structure. And that is provided by OOPs using Inheritance. Here in inheritance we have an 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 that in mind that only the public ones. Not the private methods or variables are accessible from child class. And the methods and accessible, can be call 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 bellow example.


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 example of Single Inheritance. 

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 are present. One is Sub Class 1 -> Sub Class 2 and second one is Sub Class 1 -> Super Class.

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.

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 don't support Multiple Inheritance. To achieve that we have to go for Interface, which we will discuss later.
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.

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 kept 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. Some time many data remains unused in the hierarchy. So memory wastage can be happen in case of inheritance, 

0 comments:

Post a Comment

Follow by Email

Popular Posts

Pageviews