Tuesday 28 February 2017

Inheritance in OOP

Inheritance

One of the most important concepts of object oriented programming is Inheritance. Inheritance is a mechanism of acquiring features and behaviors of one class called as base or parent class into another class called as derived class.

Inheritance implements a IS-A relationship.

Advantages of inheritance

  • Code re-usability 
  • Reduction in code size
  • Improve code readability
  • Code extensible support by overriding base class method into derived class  

Disadvantage of inheritance

  • Tight coupling between base class and child class
  • Performance of your program is affected if inheritance is not implemented properly.
  • Many unused data members remain unused & memory is allocated for the same.

Different types of inheritance

  • Single Inheritance

When a derived class is created from a single base class.
C# Example
public class Parent
{
    //Definition
}

public class Derived : Parent
{
    //Definition
}

    •  Multi-Level Inheritance

    When a derived class is created from another derived class is called as multi-level inheritance
    C# Example
    ppublic class Parent
    {
        //Definition
    }

    public class Derived1 : Parent
    {
        //Definition
    }

    public class Derived2 : Derived1
    {
        //Definition
    }

    • Multiple Inheritance

    When a derived class is created from more than one class then it is called as multiple inheritance. This type of inheritance is not supported in C# or .Net
    This type of inheritance is not supported in C# or .Net

    • Hierarchical Inheritance

    When a multiple derived class is created from one single class then it is called as Hierarchical inheritance.
     C# Example
    public class Parent
    {
        //Definition
    }

    public class Derived1 : Parent
    {
        //Definition
    }

    public class Derived2 : Parent
    {
        //Definition
    }

    public class Derived3 : Derived1
    {
        //Definition
    }

    public class Derived4 : Derived1
    {
        //Definition
    }

    public class Derived5 : Derived2
    {
        //Definition
    }

    public class Derived6 : Derived2
    {
        //Definition
    }

    • Hybrid Inheritance

    This is combination of more than one inheritance. It may be a combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or
    Hierarchical and Multilevel and Multiple inheritance.

    • Why C# does not support multiple inheritance?

    C# does not support multiple inheritance because of diamond problem. To know more about diamond problem please refer below link 
    https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem

    • As we know multiple inheritance is not supported by C# or .Net what is the other way to achieve multiple inheritance in C# or .Net? 

    We can achieve similar functionality using interface in C#.

    • Is static class can be inherited ?

    Static class can not be inherited. C# complies static class as sealed abstract class hence static class cannot be inherited in c#.

    • How to make normal class non inheritable c#?

    Using sealed keyword in c# we can stop class from further inheriting it.

    No comments:

    Post a Comment

    Custome Service Class to connect wcf service

      using  System; using  System.ServiceModel; namespace Custom.Service.Client {      // Borrowed from: http://old.iserviceoriented.com/blog/p...