Link Search Menu Expand Document

OOP in Python

Object-oriented programming is a software design conceptual model based on the concept of objects. OOP plays a vital role in development regardless of the language used for it. OOP helps the programmer to form different objects and perform different actions using them.

The concepts of OOP have been in the market since the early 1960s and have gained popularity since then. OOP is helpful in every field of today, including real-time systems, expert systems, artificial intelligence, client-server systems, object-oriented databases, and many more. Following are fundamental concepts of OOP that are beneficial to use in every program to make it more efficient.

Basic Concepts of OOP


Object-oriented programming associations groups of variables and functions into a unit called an object. The description of the class is defined with the definition of a class. Creating an object of a class is essential to allocate some memory to it. The object instance consists of the actual data and information. Object instantiation refers to the phenomenon of creating a new object or an instance of a class. An object has two characteristics.

  • Attributes
  • Behavior

e.g. A car is an object. Name, Age, and Model are the attributes. Driving, Washing is the behavior.

Obj = car()


A user-defined pattern for an object that defines a set of attributes that characterize the class objects. Simply, the class is a collection of objects. Classes help make the code more manageable. Attributes such as Class Variable and Data Member (Variables) may get initialized within the class, which holds the data associated within a class. The keyword “class” defines a class followed by the specified class name and a semicolon. A simple example of defining a class is following:

Class car:

Class is a keyword, defining an empty class car.


A mechanism in which one class possesses the property of another class having similar implementation. It refers to define a new class having full Implementation of Parent Class and add more functionality to it.

In this procedure, one class inherits all the attributes and methods of the other class. The class which provides its properties and methods in inheritance is known as the parent class. The class which inherits the properties of the parent class is known as the child class. The child class possesses all the inherited properties and methods and can have properties and methods of its own. Following is a simplified example of inheritance in Python.

class ParentClass:

  Body of ParentClass

class NewClass(ParentClass):

  Body of derived class


Multi-Level Inheritance

Multi-level inheritance refers to the type of inheritance in which a child class inherits a parent class that also has a parent class, i.e. derive some of its properties from another class. In biological terms, the child’s parent has a parent meaning the child’s grandparent.


Encapsulation is a valuable concept to ensure security. It hides the data from any unauthorised access. Encapsulation in Python can restrict access to the class, making it private for other classes. It averts data from modification. Private attributes may form to restrict the data using an underscore as the prefix ‘_’.

Only the class and its subclasses can access attributes and methods as they are protected from any other class. Moreover, the double underscore ( __ ) can declare the attributes or methods as private members to restrict their access from outside the scope of a class.

Def _init_(self):

        Self._maxprice = 500

Def MaxPrice(self, price)

Self._maxprice = price

init() function stored the maximum price and tried to change it using self ‘Object()’. As it is not accessible because it is private, and the user has no access to it. The private object is accessible changed using Setter / Getter Functions.


The term Polymorphism means to have many forms. It refers to the ability to merge different interfaces for multiple data types. As suppose a Car’s Model is available in different colors (Red, Black, White). However, the same method could be used to define its behaviors. Python allows different classes to have functions with the same name but having different functionalities, which may be generalized by disregarding the object. Following is a simple example of Polymorphism in Python.

class Audi:
   def description(self):
    	print(“description of AUDI.”)
class BMW:
 	def description(self):
    	print(“description of BMW.”)

Method Overriding

Method overriding is a phenomenon in object-oriented programming that allows a subclass or a child class to customise a method already available in the superclass or the parent class. Child classes in Python inherit techniques and attributes from the Base class. Specific methods and attributes can redefine to adequate the child class, known as Method Overriding.

Polymorphism allows users to access the overridden methods and attributes having the same name as the parent class.

Class Shape:


Class Circle(Shape):


Class Rectangle(Shape)

Note: Same Name and different Arguments are not acceptable in Python.


Abstraction is the process of concealing the real implementation of an application from the user and emphasizing its usage. The procedure hides the internal details and only shows the functionalities. Any class that possesses at least one abstract function is considered an abstract class. A programmer hides the data/process of an application to increase efficiency and reduce complexity. Abstraction is achievable by importing the class. The syntax is:


from car import

 CARClass ClassName(CAR):

There should be no implementation in the abstract class. Pass Statement is the keyword to define the Abstract Class.

from shape import SHAPE

Class Car(SHAPE):

Def total_area(self): method pass

Key Point: Abstract Class cannot have any objects.

Object-Oriented Programming makes the Application easy to understand as well as well-organized. As the class is sharable, the code can is re-usable. The program or Application is safe and secure with data abstraction. Polymorphism permits the same boundary for different objects so that programmers can write effectual code.

Other useful articles:

Back to top

© , Learn Python 101 — All Rights Reserved - Terms of Use - Privacy Policy