Warning: preg_replace(): Compilation failed: invalid range in character class at offset 4 in /home/ekhaliya/public_html/topcse/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php on line 340

Warning: Cannot modify header information - headers already sent by (output started at /home/ekhaliya/public_html/topcse/wp-content/plugins/crayon-syntax-highlighter/crayon_langs.class.php:340) in /home/ekhaliya/public_html/topcse/wp-content/plugins/sg-cachepress/core/Supercacher/Supercacher_Helper.php on line 77
Python oops concepts i.e class , objcts and its characterstics - Top CSE

Python oops concepts

in this tutorial we will learn about python oops concept , class , object etc .

python oop

reference : https://ashwinjoy.com

Python is an  object oriented  language.

A class is simply a blueprint of a data that defines the characteristics and behavior of its data members and member functions and an object is an instance of the class. therefore , Creating an object is like defining a variable of class type.

In addition , Oop to store data we use objects , and functions define in class.

When we define a class we define just a blueprint or we just map data members and member functions. A class do not  occupy any space in  memory until an object of that class type is define . in addition ,The declaration of class develops a template but data members cannot be manipulate unless the object of its type is create .

Therefore , it  allows us to develop applications using an Object Oriented approach. therefore ,In Python, we can easily create and use classes and objects.

Characteristics of oop 

  • Object
  • Class
  • Method
  • Inheritance
  • Polymorphism
  • Data Abstraction
  • Encapsulation


An object (instance) is an instantiation of a class. When we define class , only  description for  object is define. Therefore, no memory or storage allocate.

The example for object of parrot class can be:

obj = Parrot()

therefore, obj is object of class 



Suppose we have details of parrot. Now, we are going to show how to build the class and objects of parrot.

Class in python

A class is a blueprint for the object.

We can think of class as an sketch of a parrot with labels. therefore , It contains all the details about the name, colors, size etc. hence ,Based on these descriptions, we can study about the parrot. therefore , parrot is an object here .

The example for class of parrot can be :

class Parrot:  


therefore, we use


 keyword to define an empty class Parrot.  in addition From class, we construct instances. object is instance of class.


Methods are functions defined inside the body of a class. therefore ,They are used to define the behaviors of an object.

example :

class Parrot:

# instance attributes
def __init__(self, name, age):
   self.name = name
    self.age = age

# instance method
def sing(self, song):
     return “{} sings {}”.format(self.name, song)

def dance(self):
    return “{} is now dancing”.format(self.name)

# instantiate the object
blu = Parrot(“Blu”, 10)

# call our instance methods

output :

Blu sings ‘Happy’

Blu is now dancing

In addition ,  in the above program, we define two methods i.e




. therefore ,These are called instance method because they are called on an instance object i.e



Inheritance in python

Inheritance is a way of creating new class for using details of existing class without modifying it. The newly formed class is a derived class (or child class). Similarly, the existing class is a base class (or parent class).

example :

# parent class
class Bird:

    def __init__(self):
          print(“Bird is ready”)

    def whoisThis(self):

   def swim(self):
         print(“Swim faster”)

# child class
class Penguin(Bird):

     def __init__(self):
# call super() function
       print(“Penguin is ready”)

   def whoisThis(self):

   def run(self):
       print(“Run faster”)

peggy = Penguin()

output :

When we run this program, the output will be:

Bird is ready

Penguin is ready


Swim faster

Run faster

In the above program, we created two classes i.e. Bird (parent class) and Penguin (child class). The child class inherits the functions of parent class. We can see this from 


method. Again, the child class modified the behavior of parent class. We can see this from whoisThis() method. Furthermore, we extend the functions of parent class, by creating a new 



Additionally, we use 


 function before 


 method. This is because we want to pull the content of 


 method from the parent class into the child class.

Encapsulation in python

Using OOP in Python, we can restrict access to methods and variables. This prevent data from direct modification which is called encapsulation. In Python, we denote private attribute using underscore as prefix i.e single “ _ “ or double “ __“.

example :

class Computer:

     def __init__(self):
         self.__maxprice = 900

    def sell(self):
        print(“Selling Price: {}”.format(self.__maxprice))

    def setMaxPrice(self, price):
          self.__maxprice = price

c = Computer()

# change the price
c.__maxprice = 1000

# using setter function

output :

Selling Price: 900 Selling Price: 900 Selling Price: 1000

In the above program, we defined a class Computer. We use 


 method to store the maximum selling price of computer. We tried to modify the price. However, we can’t change it because Python treats the __maxprice as private attributes. To change the value, we used a setter function i.e 


 which takes price as parameter.

Data Abstraction in python

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.

Polymorphism in python

Polymorphism is an ability (in OOP) to use common interface for multiple form (data types).

Suppose, we need to color a shape, there are multiple shape option (rectangle, square, circle). However we could use same method to color any shape. This concept is called Polymorphism.


class Parrot:

    def fly(self):
        print(“Parrot can fly”)

    def swim(self):
         print(“Parrot can’t swim”)

class Penguin:

    def fly(self):
         print(“Penguin can’t fly”)

   def swim(self):
        print(“Penguin can swim”)

# common interface
       def flying_test(bird):

#instantiate objects
blu = Parrot()
peggy = Penguin()

# passing the object

output :

Parrot can fly

Penguin can’t fly

In the above program, we defined two classes Parrot and Penguin. Each of them have common method 


 method. However, their functions are different. To allow polymorphism, we created common interface i.e 


 function that can take any object. Then, we passed the objects blu and peggy in the 


 function, it ran effectively.

difference between oop and pop 

Basic Procedure/Structure oriented . Object oriented.
Approach Top-down. Bottom-up.
Basis Main focus is on “how to get the task done” i.e. on the procedure or structure of a program . Main focus is on ‘data security’. Hence, only objects are permitted to access the entities of a class.
Division Large program is divided into units called functions. Entire program is divided into objects.
Entity accessing mode No access specifier observed. Access specifier are “public”, “private”, “protected”.
Overloading/Polymorphism Neither it overload functions nor operators. It overloads functions, constructors, and operators.
Inheritance Their is no provision of inheritance. Inheritance achieved in three modes public private and protected.
Data hiding & security There is no proper way of hiding the data, so data is insecure Data is hidden in three modes public, private, and protected. hence data security increases.
Data sharing Global data is shared among the functions in the program. Data is shared among the objects through the member functions.
Friend functions/classes No concept of friend function. Classes or function can become a friend of another class with the keyword “friend”.
Note: “friend” keyword is used only in c++
Virtual classes/ function No concept of virtual classes . Concept of virtual function appear during inheritance.


%d bloggers like this: