Tuesday, December 10, 2019

[PythonOOPTutorial] 04 Object Oriented Programming Using Python

This is the forth article of the Python OOP Tutorials.
If you are new to Python, this is how to install Python on your computer.

When it comes to Object Oriented Programming, it has four main concepts:
  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism
Lets discuss those one by one.



ABSTRACTION:


Abstraction is simply hiding the real complexity of the code from the user. It hides internal implementation of the code from user; only required parts will be exposed.

For ex:

Without having any idea how engine works or ignition works, a driver can start a car. He do not need to know how battery is supplying power, how engine works, how fuel is pumping, etc... Same thing is applied here.


Above is a simple example for abstraction. If we go into more details most suitable example for abstraction can be explained using Abstract classes. You can learn more about Abstract classes from here.

For Ex:

Consider we have an abstract class Vehicle and extended class Car. In vehicle we write an abstract which is common to all the vehicles - "start" method. So we can implement that method in extended class as we want.



ENCAPSULATION:


Main idea of encapsulation is wrapping the attributes. We can use this to restrict unintentional data modifications.

For Ex:

Consider there is a Student class and a private attribute called age. So age should be always a positive value. So we can implement that as follows using object oriented programming concepts.
Here when user enters 16 as age, system allows that to be saved because 16 is a valid age. But when user enters -3 as age, system do not allows that to be saved because -3 is not a valid age; so age will remain as 16. After that when user enter 21 as age, system allows that to be saved because 21 is a valid age. So like that we can do so many things using OOP concept encapsulation.



INHERITANCE:


Inheritance is process of deriving parent class (super class) attributes and methods to its child classes (sub classes). In real life examples, we will have our parents' qualities, behaviours and features; those are inherited from parent to child. Same thing happens in Python too. When we extend a class (child class) from another existing class (parent class).

Please note private attributes and members are not inherited to child classes; because they are private. Only public and protected attributes and methods are inherited to child classes. If you want more information about Python access modifiers please refer this.

When it comes to Python Inheritance, there are two major levels:
  • Single inheritance
  • Multiple inheritance

Single Inheritance:

Basic way to define Car class is like below.
class Car:
But when we want Car class to extend Vehicle, we have to change it like below.
class Car(Vehicle):

Here Vehicle is the parent class which has two public and protected attributes. Car class is extending Vehicle class. So as I mentioned in the screenshot, vehicle attributes are accessible even from car class objects. That is because Car class is extending Vehicle class. Same thing goes with methods too.

Multiple Inheritance:

What we have discussed above is Single inheritance (those child classes inherit only one parent class). So Multiple inheritance means there can be child classes which are extended by multiple parent classes. 

A child may inherit skills from both his mother and father.
Above example describes how basic multiple inheritance will work using attributes. Here "canCook" attribute is inherited from Mother class and "canSwim" attribute is inherited from Father class.
Same applies to the methods too.

Then let us consider if both classes have the same attributes how Python is working.
Here system prints "Baby can dance". When there is a conflict among attributes or methods while multiple inheritance, system will act according to the first parent class. At this case it it Mother class.
class Baby (Mother, Father)
If we change the order of inheritance like class Baby (Father, Mother) system will not print "Baby can dance".


In addition to those there is a concept called "Multi-level Inheritance".

For Ex:

Consider below HondaCivic class; it is inherited from Car class; it is inherited from Vehicle class. This is called Multi-level inheritance and the last class at the hierarchy can access all super class attributes and methods.


POLYMORPHISM:


Polymorphism is simply having multiple formats. When it comes to Object Oriented Programming polymorphism, it has two sub concepts;
  • Overriding
  • Overloading

Overriding:


Overriding is directly related to inheritance; user can override something which is defined in super class (parent class).
Here Vehicle class has a start method. When we create a Vehicle object and call start method it will print "Vehicle starting..!!". 
Then we create a Car object and call the start method; but Car class do not have a start method. One important thing, Car class extends Vehicle class. So it can access its parent class methods. So Car object start method will also print "Vehicle starting..!!".
This is called Inheritance.

Here Car class also has a start method. When we call start method of  Car object, system will print "Car starting..!!". This is called Overriding. Here Vehicle object start method is overridden by Car object start method.


Overloading:

In normal OOP concepts Overloading means changing the input parameters without changing the method name. In Python in a single class we cannot have two methods with the same name. So for overloading we use a different technique.
Here at the Calculator class we have a method called "getSum". We can call that method with two parameters and with three parameters. When we call it with two parameters third parameter "c" will be initialized as zero (as we defined). This is called an "Optional Parameter" in Python. So nothing will happen to the summation flow. If we pass three parameters to the method, then it will return the sum of three parameters.
Like that if we want to take the multiplication value we have to initialize value of "c" to one (c=1) to get the correct answer. So always you have to initialize the optional values carefully. Otherwise you will not get the expected outcome.


So these are the basic Object Oriented Programming concepts using Python. Hope this is useful for you.


Cheers...!!

1 comment: