18 May 2013

inheritance-in-oops

Inheritance is one of the main features of Object Oriented Programming concepts. Inheritance plays an important role in object oriented design. Let's go through the below sections to understand inheritance in OOPS:

What is Inheritance in OOPS?

Inheritance allows the derived classes to use features / functionality of the base class and also defines the relationships between the two objects. Code re-usability is one of the benefits of inheritance.

Inheritance examples in C# .NET

Best way to understand inheritance is to look at real world examples. For instance in animal kingdom Homosepeans have characteristics of mammals. In OOPS terminology human beings are inherited from the mammals. So mammals are base class and human beings are derived class.

Let's consider a real world inheritance example using the below C# class sample.

namespace InheritanceTutorial
{
  public class Doctor
  {
    public void DiagnosePatient() { }
    public void PrescribeMedicine() {}
  }
  
  public class Surgeon:  Doctor
  {
    public void DoSurgery() { }
  }
    
  public static void Main(string[] args)
  {

    Surgeon surgeon = new Surgeon();

     // Derived class instance invoking base 
     // class methods.
     // There by inheritance helps in code re-usability 
     // and also proper relationship 
     // between the classes.
     surgeon.DiagnosePatient();
     surgeon.PrescribeMedicine();

    surgeon.DoSurgery();
   }
}

Analogy of the above inheritance tutorial example:

  1. Doctor and Surgeon are two classes.
  2. Surgeon class inherits from the Doctor class. Here Doctor is base class and Surgeon is derived class.
  3. Derived class Surgeon is making use of the code present in the base class Surgeon. Derived classes customize base class behavior by overriding them or define new behaviors.
  4. Inheritance not just helps in code re usability. Using inheritance we can define "IS A" relationship.

Inheritance defines "IS A" relationship:

Considering the above inheritance example Surgeon IS A Doctor. Do not implement inheritance relationship for the sake of code re-usability. Always look out for IS A relationship. Your class design using inheritance makes sense only if every derived class exhibits IS A relationship with their base classes.

UML representation for OOPS inheritance:

In UML, OOPS inheritance will be represented by generalization relationship. In UML representation of inheritance, there a solid line running from derived class to base class with a closed arrowhead. Below UML diagram shows how to represent inheritance relationship between classes.

What do consider when implementing inheritance:

  1. Your proposed derived class should represent IS A relationship with the base class. If there is NO IS A relationship, then inheritance is not the right option. Don't try to fit the inheritance everywhere.
  2. Depth of Inheritance indicates how many classes are present in its inheritance hierarchy. It would be difficult to maintain the code if your inheritance class hierarchy chain is very long.
  3. Inheritance is static in nature unlike polymorphism. At design time only, the relationship between the base class and derived class is fixed. And the behavior what derived class exhibits is also fixed.

Design pattern based on Inheritance:

Template method design pattern makes use inheritance. Derived classes in Template design pattern customize one or more algorithm steps behavior, which are defined in the base class.

No comments:

Post a Comment