Understanding Inheritence - Part 1

In the object oriented world, just like the real world, objects follow a parent child relationship and this is what we call as inheritance. The goal of inheritance is simple – to help avoid duplicate code. By using the inheritance principle you can write the common code which is to be used by many objects into a common class and then inherit all other classes from that class. The main terms associated with inheritance are

  1. Parent or base class
  2. Child or derived class

The parent class is the one which contains the common code. The child class is the one which inherits itself from the parent class and receives all the features from it. It is like when a child is born in the real world it inherits features from his/her parents and additionally it also has his/her own features.

Inheritance can be a bit complex to understand at the beginning. Not because the concept is difficult to understand but because without much coding experience using object oriented languages you will not be able to realize its actual use. Inheritance is something you will get used to as you gain more experience. So just understand the concept and put it to use slowly and steadily as you gain more experience with object oriented programming.

An example to explain inheritance

As you just read above that the goal of inheritance is to avoid duplicate or repetitive coding. When you write the same code over and over again it becomes difficult to manage and maintain it. So our goal when writing software projects should be to avoid doing repetition by writing the same code over and over again inside different classes.

For example let’s say that you created two classes A and B with the following functionality

Class A
{

Function1()
{
    // 500 lines of code
}

Function2()
{
    // 100 lines of code.
}

}

Class B
{

Function1()
{
    // 500 lines of code.
    // Exactly similar to Function1 in class A
}

Function3()
{
    // 100 lines of code.
}

}

Notice in this code that Function1 has 500 lines of code as mentioned in the comment written inside Function1 body. Also both classes A and B have Function1 in them and the exact same lines of code have been repeated in both of them. The principle of inheritance says that if you have the same code in two or more classes then you should create a common class for them and put the repeated code in the common class.

Look at the code below. We have created a new class C which will act as the common class for both classes A and B.

Class C
{

Function1()
{
    // 500 lines of code.
    // Exactly similar to Function1 in class A
}

}

Class A inherits C
{

Function2()
{
}    

}

Class B inherits C
{

Function3()
{
}    

}

We have now created a new class C and copied the Function1() into this class. We can now use this C class as our parent or base class. Look at the code changes for classes A and B. Now both classes A and B inherit themselves from class C and also we have removed the Function1() from these classes. Because of the fact that they are inheriting themselves from class C they will automatically get Function1 from their parent class. By doing this we have remove the code repetition by moving the Function1() code inside the common class.

Multiple inheritance

In the real world when a child is born it inherits the features of both parents. This is what we call as multiple inheritance. Similarly in object oriented programming you can inherit a class from two parent classes.

Multi level inheritance

In the real world a child inherits features his father and his son inherits the features from him. We say that the same quality runs in the whole family, from grandfather to father and then to the son. This is called multilevel inheritance.

Don’t overuse inheritance!

Inheritance should be used only where it makes sense. Do not try to over simplify the code by using inheritance. Overuse of inheritance can make things complicated rather than making them simple. That is why we said in the beginning of this tutorial that inheritance is something you learn with experience.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp