Python oops concepts with example, python oop attributes, python oops concepts

Python OOPs concepts with example

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.

Python OOPs Concepts

Object Oriented Programming in Python , Here you learn about following four pillars of oops with some examples

Inheritance in Python Example

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)

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
    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();  

Notice, we are calling student.personName(), but personName() method is actually written in base class "People"

Multiple inheritance in Python

Python also supports multiple inheritance, we can inherit from multiple base class like MyClass(BaseClass1, BaseClass2)

Abstraction in Python Example

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

    def start(self):
         ''' data '''

    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 in Python Example

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):
        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


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 in Python

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");

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): 


In above example, we are calling the function method with different parameters (different type of objects) Maruti and Honda

Python OOPs
Object Oriented Programming in Python
Python programming examples | Join Python Course