Wednesday, May 25, 2016

An Introduction to Object Oriented Programming in Python - Part 7

Inheritance in Python OOP

Make sure you read Part 1, Part 2, Part 3, Part 4, Part 5 and Part 6 before you continue reading...

In python OOP, Classes can inherit functionality (methods) and properties (attributes) from other classes. Inheritance is a powerful feature in object oriented programming.

Python supports inheritance, it even supports multiple inheritance. Classes can inherit from other classes. A class can inherit attributes and behavior methods from another class, called the superclass.

Inheritance is explain in other words to be a definition of a new class with little or no modification to an existing class. It is a way to form new classes using classes that have already been defined.

The newly formed classes are called derived (or heir or child) classes, the classes that we derive from are called base (or super or parent) classes.

Important benefits of inheritance are
~ code reuse and
~ reduction of complexity of a program.

The derived class inherits features (both attributes and methods) from the base class, adding new features to it.

It should be noted that: objects only inherit the parent class methods and attributes, not instance attributes or methods.

Let’s take a look at how Inheritance works using a clear real world object (our Cars object).

General Syntax is:

class BaseClass: 
 body_of_base_class

class DerivedClass(BaseClass): 
 body_of_derived_class

class A: 
 body_of_base_class

class B: 
 body_of_base_class

class C(B): 
 body_of_derived_class

# multiple inheritance
class D(A, B): 
 body_of_derived_class

# multi level inheritance
class E(C, D): 
 body_of_derived_class




Note that the inheritance structure could be complicated in the sense that we could have, multiple and multi-level inheritance structure as seen in classes D and E above respectively.

There exists a hierarchy relationship between classes. It's similar to relationships or categorizations that we know from real life. Think about our Cars object, which is a type of Vehicles for example.

We could create inheritance relationship by considering other types of Vehicles such as: Bikes, Buses, Trucks etc. Thereby allowing us have a super/parent class of Vehicles and other derived/child classes of Cars, Bikes, Buses, and Trucks. These child classes can also have further child classes allowing us have multiple and multi-level inheritance structure in our code.



A minimal code to demonstrate the above inheritance structure will look like this:-

class Vehicles:
 def __init__(self):

class Cars(Vehicles):
 pass

class Bikes(Vehicles):
 pass

class Buses(Vehicles):
 pass

class Trucks(Vehicles):
 pass

From the code above, all the derived/child classes of Cars, Bikes, Buses, and Trucks will inherit the attributes and methods (including the constructor method) of the super/parent class Vehicles.

If you want a child class to keep the inherited constructor method of the super/parent class and yet have it own constructor method, then you make use of super() method or create the constructor method explicitly.

Here is how the code will be:-

class Vehicles:
 def __init__(self):
  self.brand = brand

class Cars(Vehicles):
 def __init__(self):
  super(Cars, self).__init__(self)

class Bikes(Vehicles):
 def __init__(self):
  Vehicles.__init__(self)

class Buses(Vehicles):
 pass

class Trucks(Vehicles):
 pass

Both ways child classes Cars and Bikes inherited the constructor method of the super/parent class will work fine. The difference is that Cars way is "Implicit" while Bikes way is "Explicit".

However, using the super() method as used by Cars class has this advantage that when ever the super/parent class name is changed, it will still work fine. Whereas that of Bikes class will fail with change in super/parent class name.

That is it about inheritance, lets talk about Polymorphism in the next Part 8.


Reference Materials are here

No comments:

Post a Comment