Inheritance

One of the most important reasons of using Object Oriented Programming is our improved code writing efficieny. As you must have seen so far, Classes and objects can do wonders when it comes to certain tasks. Another addition in making this better, is the concept of Inheritance. This works really well for most cases. As the name goes, we inherit something that falls under the same cateogory. In other words, type and sub-type. Sub-types can inherit from types the information they need to possess. Let's say you have a class named Family. We could keep a sub class for Parents and another for Children with different attributes for each other. This saves the time of writing 2 completely different Classes. There are many other advantages to this as well. Think about it for a minute.

Imagine you want to make a change in the Family Class, it will automatically show in the members of this class, so it saves extra work there to update the sub classes. On the other hand, changes in the sub classes won't change the Family Class, which is extremely useful. The kid of the family can set a Boolean variable for graduation in the Children Class which won't be needed for the Parents Class.

There is one more advantage. What if you want to calculate all the members in a Family? You can simply refer to the family members as objects of the Family Class and count them easily. This is referred to as Polymorphism. It means that any sub-class type can be replaced in any situation where we expect a head-class type. Or in other words, as we just said, the object of the sub-class can be used as the object/instance of the head class. It might seem a bit off for a small family, but consider the case of having 1000 employees. They could be divided into various cateogories depending upon their level in the hierarchy and still be referred to using the Head class.

In more formal terms, the Family class in our example is the superclass or the base class. The Children and Parent class is the subclass or derived class. Once again, talking about the term inheritance, Children and Parent class inherit from the Family class.

To make this much clearer, we will do an example. You can use the white space on your Repl editor and RUN the code.

class Family:

    def __init__(self,name):
      self.name = name
      print('Family member added')

    def introduce(self):
      print('The name is '+self.name + '.')

class Parent(Family):

    def __init__(self,name,role):
      Family.__init__(self,name)
      self.role = role
      print('Added parent')

    def introduce(self):
      Family.introduce(self)
      print('Role: '+ self.role)

class Kid(Family):

    def __init__(self,name,role):
      Family.__init__(self,name)
      self.role = role
      print('Added kid')

    def introduce(self):
      Family.introduce(self)
      print('Role: '+ self.role)

p = Parent('Mr. Davies', 'Dad')
k = Kid('Daniel', 'Son')

print() # -> to print a blank line

p.introduce()
print()
k.introduce()

This would output:

Family member added
Added parent
Family member added
Added kid

The name is Mr. Davies.
Role: Dad

The name is Daniel.
Role: Son

That was easy! We wrote a program with Family as the superclass and Parent, Kid as the subclass. The way we did this, is by passing Family as an argument in class initialization: class Parent(Family): and class Kid(Family):. This simply means that Parent and Kid inherit from Family. To make the concept clear, we used one method named introduce() which introduces the added member and tells their role. This statement Family.introduce(self) helps us treat the objects of both Parent and Kid as instances of Family.

TIP: A suggestion here would be to rewrite the code once again to check the level of understanding.

results matching ""

    No results matching ""