In this tutorial you will learn OOPs concepts in Python, If you have some understanding of how to create python class and objects that will be very helpful for this tutorial.
Object Oriented Programming in Python , Here you learn about following four pillars of oops with some examples
Here in example "Student" class is inherited from People class, this is an example of Inheritance in python, ChildClass(BaseClass)
from School.People import People #this is an example of inheritence class Student(People): def __init__(self): self.Title = "Student"; def welcomeMessage(obj): print ("Welcome " + obj.first_name) pass
Here is the base class definition, all properties are written in base class and a method called personName()
class People(object): def __init__(self, fname, lname): self.first_name = fname self.last_name = lname self.Mobile; self.Address; self.Email; def personName(self): print(self.first_name, self.last_name)
Now in Main class we create a new instance of Student class and access the method of base class.
from School.Student import Student class Main(object): student = Student(); student.first_name="Michael"; student.last_name="Collins"; student.personName();
Notice, we are calling student.personName()
, but personName() method is actually written in base class "People"
Python also supports multiple inheritance, we can inherit from multiple base class like MyClass(BaseClass1, BaseClass2)
Abstraction in Python: All abstract class has to implemented from ABC (Abstract Base Classes).
We need import library abc, and all abstract method has to have @abc.abstractmethod
attribute, and cannot have implementation
Here is the declaration of Abstract Class with two abstract methods, you need to set the __metaclass__ = abc.ABCMeta
import abc class AbstractCar(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def start(self): ''' data ''' @abc.abstractmethod def stop(self): ''' data '''
Remember you cannot create a new instance of any abstract class
Now let's see how to implement abstract class in any child class, here in example we create a class called Maruti.
class Maruti(Car): def __init__(self, carModel): print('Maruti created.') self.CarModelName = carModel def start(self): print('Maruti car started', self.CarModelName) def stop(self): print('Maruti car stopped', self.CarModelName)
When you implement any abstract class in any child class, you must implement all abstract methods those are declared in base abstract class. like here in Car class we had two methods declaration, start and stop, and both methods were implemented in Maruti class.
Encapsulation is the technique of hiding object members like variables, properties method etc within the object, so no one can access those properties / methods from outside, like any other programming language we can achieve this by using different access modifiers.
All members in python object are public by default, but like any other object programming language we can set private(__)
and protected(_)
members
In below Maruti class we have four methods two are public and one private (__checkEngine)
and one protected (_checkOil)
class Maruti(Car): def __init__(self, carModel): print('Maruti created.') self.CarModelName = carModel def start(self): self._checkOil() self.__checkEngine() print('Maruti car started', self.CarModelName) def stop(self): print('Maruti car stopped', self.CarModelName) def __checkEngine(self): print('Checking engine', self.CarModelName) def _checkOil(self): print('Checking oil', self.CarModelName)
Private : any member start with Double underscore (__)
is private member, like __checkEngine()
method in above code
We cannot access private members outside the class, for example if we try to access the method __checkEngine
from outside, it will throw exception
cr=Maruti("Suzuki"); cr.checkEngine()
the above code will throw exception "'Maruti' object has no attribute '_Main__checkEngine'"
Protected : any member start with Single underscore (_)
is protected member, like _checkOil()
method in above code
Polymorphism is very interesting feature, which helps writing meaningful, easily maintainable code for any business operation; Polymorphism means the ability to have various forms of same function, means same function can be executed with different parameters in different situation.
Writing same function in multiple forms with different parameter is called method overloading.
Here is simple example of Print method
print("hello world"); print(1); print(1,2,3)
The method accepts different type of parameters, string, number, array etc. Method name is same, but has different forms.
Here is anothe example of Polymorphism with custom objects and a Function
def func(obj): obj.start() mCar=Maruti("Suzuki"); hCar=Honda("Civic"); func(mCar) func(hCar)
In above example, we are calling the function method with different parameters (different type of objects) Maruti and Honda