Object Oriented Programming in Python

As in Python it is not necessary to create our code into classes while developing program as we can use functions also which is known as Procedural Programming. However, a procedural programming is used for writing small, short and simple programs whereas object oriented programming (OOP) programs becomes more important as the programs grows in size and complexity. The Python has been the object oriented language since it is developed.

So let’s have small introduction about object oriented programming concepts used-

Classes

In comparison to other programming languages, the concept of classes in Python is added with minimum of new syntax and semantics. The concept of classes in Python is a mixture of classes in C++ and Modula-3. The Python classes provide all the basic features of OOP such as class inheritance which allows multiple base classes, a derived class which can override any method of its base class and the method which can call the method of a base class with the same name.

First look at classes

In Python classes are introduced with new syntax and semantics.

Class definition syntax:

class ClassName:

<statement 1>

<statement 2>

.

.

.

<statement n>

Example:

Given below is the example of a simple Python class:-

class Student:       //common base class for all students

stuCount=0

def_init_(self, name, rollno):

   self.name = name

   self.rollno = rollno

Student.stuCount += 1

def displayCount( self ):

     print “ The number of students are: %d ” % Student.stuCount

def displayStudent( self ):

print “Name : ” , self.name , “, Roll No : ” , self.rollno

In the above code the variable stuCount is a class variable whose value is shared among all instances of a student class. The variable can be accessed as Student.stuCount from inside the class or outside of the class. The first method init( ) is a special method which is called class constructor or the initialization method which is called when you create a new instance of the class. The self argument is added by the Python itself when the methods are called.

Objects

Objects are the basic building block of Python object oriented program.

stu1 = Student(“Raj” , 34) // first object of student class

stu2 = Student(“Reema” , 12) //second object of student class

Attributes

Attributes is the feature of an object. The _init_( ) method is used to initialize the attributes of an object. To access the object’s attributes we use the dot operator with the object. Like

stu1.displayStudent( )

stu2.displayStudent( )

print “The number of students are: %d ” % Student.stuCount

So the complete program is:

class Student:       //common base class for all students

stuCount=0

def_init_(self, name, rollno):

   self.name = name

   self.rollno = rollno

Student.stuCount += 1

def displayCount( self ):

     print “ The number of students are: %d ” % Student.stuCount

def displayStudent( self ):

print “Name : ” , self.name , “, Roll No : ” , self.rollno


stu1 = Student(“Raj” , 34) // first object of student class

stu2 = Student(“Reema” , 12) //second object of student class

stu1.displayStudent( )

stu2.displayStudent( )

print “The number of students are: %d ” % Student.stuCount

Inheritance

Inheritance another feature of an OOP in Python where it a way to build new classes from the existing classes and they are named as derived classes. The derived classes are derived or inherited from base classes. The main advantage of inheritance is that the code can be reused and the complexity of the program can be reduced. The derived classes extends the functionality of base class.

Example:

class Vehicle:           // base class

   def _init_( self ):

       print “Calling vehicle class”

   def vehicle1( self ):

       print “Calling vehicle1 method”

class Car(Vehicle):         //derived class

       def _init_(self):

           print “Calling car class”

       def car1(self):

           print “Mercedes,BMW,”

c= Car( )   //instance of car class

c.car1( )   //calling derived class method

c.vehicle1( ) //calling base class method

Output:

Calling car class

Mercedes,BMW

Calling vehicle class

Calling vehicle1 method

Polymorphism

The polymorphism is a process where a function is used in different ways for different inputs. Basically, polymorphism is if a class B is inherited from class A so it does not inheriting everything of class A it can inherit some of the functions of class A.

Example:

class Books:

   def _init_(self, name=‘ ’):

           self.name = name

   def programming(self):

           print “Programming books:”

class Python(Books):

   def programming(self):

         print “In python world”

   class Java(Books):

       def programming(self):

           print “In java world”

b = Books( )

b.programming( )

p = Python( )

p.programming( )

j = Java( )

j.programming( )

Output:

Programming books:

In python world

In java world

Operator Overloading

In Python classes can have the operations with the special method names but these methods cannot be called directly but by a specific syntax.

Example:

class Addition:

   def _init_(self,a,b):

         self.a = a

         self.b =b

def _str_(self):

     return ‘Addition (%d, %d) ’ % (self.a, self.b)

def _add_(self,other):

     return Addition(self.a + other.a, self.b + other.b)

a1 = Addition(5,10)

a2 = Addition(2,3)

print a1 + a2

 

2 Comments Object Oriented Programming in Python

  1. meenal deshpande

    Good informative article.
    The explaination that you have given with example its really easy to underastand.I like to read your blogs and I am regular follower of your blogs.
    Thanks for the brief explianation about oops concept as many of us confuse between c and cpp..

    Reply

Leave A Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.