In the last article (Object Oriented Programming in Python – Classes and Objects); we talked about the main benefits for using OOP approach in programming. One of these benefits was the Inheritance. Inheritance means the capability to reuse an existing class, and “extend” its features. (Actually some programming language like Java use the keyword extend to define the inheritance in code).
What is Inheritance?
To get the idea of inheritance closer to your mind, let’s take a real life example. Consider a farmer having a nice farm in the Indian countryside. This man has been working in agriculture for about 50 years. Of course, he gained a great experience. His son, who worked with his father for years, has just graduated from faculty of Agriculture. Now, the son has his father’s great practical experience, plus scientific knowledge and education. After years, the son who had become a father, has educated his son (for accuracy a grandson) the agriculture, transferring the experience he gained over years from working with his father (who is now a grandfather), combined with the scientific knowledge he gained from his education. Now, the grandson who has just completed his masters in Agriculture, has his grandfather’s practical experience, plus his father’s practical and scientific knowledge, plus his advanced knowledge gained from studying the masters. Have you got the idea?!
You see how inheritance has enriched the whole gain!! Similarly, consider I wrote a class, tested it, and then saved it in a .py file. From your point of view, you see that this class would be much better and more useful if you add one attribute to store the value of x and two member functions (methods) to enhance the data processing. You could do that easily with inheritance, without the need to re-write the class from scratch. This allows for cooperation, and enriches the computer programming field. (The idea of open source software depends on nearly the same concept).
* * * * * *
After illustrating the concept, now to the real work!! The syntax for inheriting a class is as follows:
class sub-class-name (super-class-name):
where: sub-class-name is the name of the new child class, and
super-class-name is the name of the original (parent) class.
Starting from the Point class example (that we have seen in the previous article), we want to inherit this class to extend its features.
In this example, we are going to define a new class called NewPoint that inherits the Point class.
The subclass will add its own member function draw() that simulates the drawing operation.
Now, let’s use the new child class NewPoint, by defining an instance (object) of it:
To initialize the two member variables x and y, the setcoordinate method should be used. Remember that this method is inherited from the parent class Point.
As you have already guessed, the member function (method) getcoordinates is also accessible and available for use:
The newly-defined method draw, which was defined in the subclass NewPoint can be used to draw the point p.
Here is the complete script:
In the previous example, NewPoint subclass has access to all the attributes and methods defined in its parent class Point, exactly the same way that the son (in the Farm example) has access to its parent (grandfather) long experience. Just like the son’s scientific knowledge he gained from his college study (that his parent doesn’t have), the child class has also its own exclusive method draw that it has defined. So, instances of the Point class don’t know anything about the draw method, while instances of the child class NewPoint do.
Now, let’s take another example to get our hands familiar to writing classes.
In this example, we are going to define a class called Math, which has two numeric member variables x and y. The class should define two member functions add and subtract which do exactly what their names tell. After that, a new subclass called AdvMath will inherit the Math class and extends its functionality with new methods like multiply, divide, and power.
Okay, let’s do it!!
– Okay, everything seems normal and as usua… oops, what is this?! __init__?!
Yeah, I was about to tell you about it. The __init__ method acts like a constructor. If you have some knowledge in programming using C++ or Java, you would know that constructors are special functions, which are executed automatically when an object is first created. Constructors provide a way to initialize the object on creation, instead of using set functions. In the code above, the __init__ function initializes both member variables x and y to the values provided by the user when defining the object. Notice that this way, we didn’t have to define a setx and sety functions.
Now, let’s define an instance of the Math class.
Strange! Huh?! Actually, nothing strange, the __init__ function will be called and given the two parameters 5, and 8 as its arguments. This way the x and y variables are initialized.
It works as expected. Now, let’s extend it by inheritance.
Create an instance and initialize it:
Now, test the available operations:
Again, both methods coming from the parent class (Math) and others defined in the subclass (AdvMath) are available to instances of the child class AdvMath.
The complete script is shown below:
* * * * * *
In this Article, we have talked about the Inheritance as one of the main benefits of using Object Oriented Programming approach. We have seen that inheritance increases code reusability, and enriches the developing process by allowing developers to share classes they wrote with others who can simply extend (inherit) them and add their own methods and data. We have also learned about constructors that facilitates the object initialization process. We have illustrated the tough theoretical concept by two examples.
I hope you find this article useful.