0% found this document useful (0 votes)
8 views7 pages

Introduction To OOPs in Python2

Uploaded by

SANJAY
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
8 views7 pages

Introduction To OOPs in Python2

Uploaded by

SANJAY
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

UNIT-V

Object Oriented Design

5.2 Introduction to Object Oriented Programming


Python is an object-oriented programming language. It allows us to develop applications using
Object Oriented approach. In Python, we can easily create and use classes and objects.

Major principles of object-oriented programming system are given below

 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.

Object-oriented vs Procedure-oriented Programming languages


Index Object-oriented Programming Procedural Programming
Object-oriented programming is an problem
Procedural programming uses a list of
1. solving approach and used where computation is
instructions to do computation step by step.
done by using objects.
In procedural programming, It is not easy to
2. It makes development and maintenance easier. maintain the codes when project becomes
lengthy.
It doesn't simulate the real world. It works on
It simulates the real world entity. So real world
3. step by step instructions divided in small parts
problems can be easily solved through oops.
called functions.
It provides data hiding. so it is more secure than
Procedural language doesn't provide any proper
4. procedural languages. You cannot access private
way for data binding so it is less secure.
data from anywhere.
Example of object-oriented programming Example of procedural languages are: C,
5.
languages are: C++, Java, .Net, Python, C# etc. Fortran, Pascal, VB etc.

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.

Python Inheritance Syntax

class DerivedClassName(BaseClassName):
<statement-1>
. . .
<statement-N>

Python Inheritance Syntax 2

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.

Python Inheritance Example


Let's see a simple python inheritance example where we are using two classes: Animal and Dog.
Animal is the parent or base class and Dog is the child class.

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...

Python Multilevel Inheritance


Multilevel inheritance is also possible in Python like other Object Oriented programming
languages. We can inherit a derived class from another derived class, this process is known as
multilevel inheritance. In Python, multilevel inheritance can be done at any depth.

4
Image representation:

Python Multilevel Inheritance Example

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.

Python Multiple Inheritance Syntax

class DerivedClassName(Base1, Base2, Base3):


<statement-1>
.
.
.
<statement-N>
Or
class Base1:
pass

class Base2:
pass

class MultiDerived(Base1, 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")

class Third(Second, First):


def __init__(self):
super(Third, self).__init__()
print("third")
Third();

Output:

first
second
third

Why super () keyword


The super() method is most commonly used with __init__ function in base class. This is usually
the only place where we need to do some things in a child then complete the initialization in the
parent.

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.

You might also like