Object Oriented Programming in Python Part-2
In this chapter, we will discuss following topics:
- Inheritance
- Encapsulation
- Polymorphism
Inheritance
Inheritance is a way to form new classes using classes that have already been defined without modifying it. Newly formed class is the Derived class(Child Class) and the existing class is the Base Class(Parent Class).
In real life, Roaring is a property of Lion so does Cub. It means, Cub has inherited the Roaring characteristic of Lion.Technically speaking, Cub is Derived Class and Lion is Base Class.
Let’s see an example:
class Animal:
def __init__(self):
print("Animal Class Created...")
def eat(self):
print("Animal is Eating")
def run(self):
print("Running")class Dog(Animal):
def __init__(self):
Animal.__init__(self)
print("Dog created...")
def eat(self):
print("Dog is Eating")
def bark(self):
print("Dog is barking")d = Dog()
d.eat()
d.bark()
d.run()Output:Animal Class Created...
Dog created...
Dog is Eating
Dog is barking
Running
We have created two classes, Animal(Base Class) and Dog(Derived Class).
- Dog class is inheriting the property of Animal Class by using run() method of Animal Class.
- Dog class is modifying the the behavior of Animal Class. Look at eat() method.
- Dog also extending the function of Animal Class, look at run() function.
We have used Animal before __init()__ method because we want to pull the content of __init()__ method from the parent class into the child class.
Encapsulation
We can restrict the access to the methods and Variables in a class, this is achieved by Encapsulation. We can prevent the methods and variables from direct modification.
Look at following example:
class Dog:
def __init__(self):
self.food="Roti"
def eat(self):
print("Dog is eating {}".format(self.food))d = Dog()
d.eat()d.food="Meat"
d.eat()Output:
Dog is eating Roti
Dog is eating Meat
We have created the Dog class. __init()__ stores the food variable.
We tried to change the value of attribute, food:
d.__food="Meat"
Value changed. We can directly change the attribute value of a class. Can we restrict this ? So, here comes the Encapsulation.
One of the way to achieve this is to make a variable private and this can be done by prefixing a single underscore( _ ) or double underscore ( __ ).
class Dog:
def __init__(self):
self.__food="Roti"
def eat(self):
print("Dog is eating {}".format(self.__food))
d = Dog()
d.eat()d.__food="Meat"
d.eat()Output:
Dog is eating Roti
Dog is eating Roti
We have created the Dog class. __init()__ stores the food variable.
We tried to change the value of attribute, food:
d.__food="Meat"
But, value didn’t change. This is because we have made food attribute as private and that’s why we cannot change the value of food directly.
Now, the question is, can we change the value of private attribute ?
Yes and for that we have to use a setter function.
Look at the following example,where we are changing value of food using setter function setFood().
class Dog:
def __init__(self):
self.__food="Roti"
def eat(self):
print("Dog is eating {}".format(self.__food))def setFood(self,food):
self.__food=foodd = Dog()
d.eat()d.setFood("Meat")
d.eat()Output:
Dog is eating Roti
Dog is eating Meat
Polymorphism
Polymorphism refers to the way in which different object classes can share the same method name.
Let’s have a look on following example:
class Dog:
def __init__(self,food):
self.food=food
def eat(self):
print('Dog is eating {}'.format(self.food) )
class Cat:
def __init__(self,food):
self.food=food
def eat(self):
print('Cat is eating {}'.format(self.food) )
d = Dog('Roti')
c = Cat('Meat')
d.eat()
c.eat()
Output:
Dog is eating Roti
Cat is eating Meat
Dog class and a Cat class, and each has a eat() method. When called, each object's eat() method returns a result unique to the object.
We can use one more function by passing an object as a parameter to obtain object-specific results.
class Dog:
def __init__(self,food):
self.food=food
def eat(self):
return 'Dog is eating '+self.food
class Cat:
def __init__(self,food):
self.food=food
def eat(self):
return 'Cat is eating '+self.fooddef animal_eat(animal):
print(animal.eat())
d = Dog('Roti')
c = Cat('Meat') animal_eat(d)
animal_eat(c)Output:
Dog is eating Roti
Cat is eating Meat
Here, animal_eat() function takes an object as a parameter. If we pass object c then it will fetch the function of Cat class and if we pass object dthen it will fetch the function of Dog class.
That’s it!
Now, you will be able to create your own objects and classes in Python.
Happy Coding!