Introduction To OOPs in Python2
Introduction To OOPs in Python2
Object
Class
Method
Inheritance
Polymorphism
Data Abstraction
Encapsulation
Object
Object is an entity that has state and behavior. It may be anything. It may be physical and logical.
For example: mouse, keyboard, chair, table, pen etc.
Everything in Python is an object, and almost everything has attributes and methods. All
functions have a built-in attribute __doc__, which returns the doc string defined in the function
source code.
Class
Class can be defined as a collection of objects. It is a logical entity that has some specific
attributes and methods. For example: if you have an employee class then it should contain an
attribute and method i.e. an email id, name, age, salary etc.
Syntax:
class ClassName:
<statement-1>
.
.
.
<statement-N>
Method
Method is a function that is associated with an object. In Python, method is not unique to class
instances. Any object type can have methods.
1
Inheritance
Inheritance is a feature of object-oriented programming. It specifies that one object acquires all
the properties and behaviors of parent object. By using inheritance you can define a new class
with a little or no changes to the existing class. The new class is known as derived class or child
class and from which it inherits the properties is called base class or parent class. It provides re-
usability of the code.
Polymorphism
Polymorphism is made by two words "poly" and "morphs". Poly means many and Morphs means
form, shape. It defines that one task can be performed in different ways. For example: You have
a class animal and all animals talk. But they talk differently. Here, the "talk" behavior is
polymorphic in the sense and totally depends on the animal. So, the abstract "animal" concept
does not actually "talk", but specific animals (like dogs and cats) have a concrete implementation
of the action "talk".
Encapsulation
Encapsulation is also the feature of object-oriented programming. It is used to restrict access to
methods and variables. In encapsulation, code and data are wrapped together within a single unit
from being modified by accident.
Data Abstraction
Data abstraction and encapsulation both are often used as synonyms. Both are nearly synonym
because data abstraction is achieved through encapsulation.
Abstraction is used to hide internal details and show only functionalities. Abstracting something
means to give names to things, so that the name captures the core of what a function or a whole
program does.
2
[5.3 Inheritance
Inheritance is a feature of Object Oriented Programming. It is used to specify that one class will
get most or all of its features from its parent class. It is a very powerful feature which facilitates
users to create a new class with a few or more modification to an existing class. The new class is
called child class or derived class and the main class from which it inherits the properties is
called base class or parent class.
The child class or derived class inherits the features from the parent class, adding new features to
it. It facilitates re-usability of code.
Image representation:
The following are the syntax to achieve inheritance. We can either pass parent class name or
parent class name with module name as we did in the below example.
class DerivedClassName(BaseClassName):
<statement-1>
. . .
<statement-N>
class DerivedClassName(modulename.BaseClassName):
<statement-1>
.
. .
<statement-N>
3
Parameter explanation
The name BaseClassName must be defined in a scope containing the derived class definition. We
can also use other arbitrary expressions in place of a base class name. This is used when the base
class is defined in another module.
Here, we are defining eat() method in Animal class and bark() method in Dog class. In this
example, we are creating instance of Dog class and calling eat() and bark() methods by the
instance of child class only. Since, parent properties and behaviors are inherited to child object
automatically, we can call parent and child class methods by the child instance only.
class Animal:
def eat(self):
print 'Eating...'
class Dog(Animal):
def bark(self):
print 'Barking...'
d=Dog()
d.eat()
d.bark()
Output:
Eating...
Barking...
4
Image representation:
class Animal:
def eat(self):
print 'Eating...'
class Dog(Animal):
def bark(self):
print 'Barking...'
class BabyDog(Dog):
def weep(self):
print 'Weeping...'
d=BabyDog()
d.eat()
d.bark()
d.weep()
Output:
Eating...
Barking...
Weeping
5
Python Multiple Inheritance
Python supports multiple inheritance too. It allows us to inherit multiple parent classes. We can
derive a child class from more than one base (parent) classes.
Image Representation
The multiderived class inherits the properties of both classes base1 and base2.
class Base2:
pass
6
Python Multiple Inheritance Example
class First(object):
def __init__(self):
super(First, self).__init__()
print("first")
class Second(object):
def __init__(self):
super(Second, self).__init__()
print("second")
Output:
first
second
third
Example:
class Child(Parent):
def __init__(self, stuff):
self.stuff = stuff
super(Child, self).__init__()
Composition in Python
Composition is used to do the same thing which can be done by inheritance.