Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full potential of Python OOP capabilities to design elegant and efficient solutions to complex problems.
OOPs is a way of organizing code that uses objects and classes to represent real-world entities and their behavior. In OOPs, object has attributes thing that has specific data and can perform certain actions using methods.
OOPs Concepts in Python
- Class in Python
- Objects in Python
- Polymorphism in Python
- Encapsulation in Python
- Inheritance in Python
- Data Abstraction in Python
Python OOPs ConceptsPython Class
A class is a collection of objects.Classesare blueprints for creating objects. A class defines a set of attributes and methods that the created objects (instances) can have.
Some points on Python class:
- Classes are created by keyword class.
- Attributes are the variables that belong to a class.
- Attributes are always public and can be accessed using the dot (.) operator. Example: Myclass.Myattribute
Creating a Class
Here, the class keyword indicates that we are creating a class followed by name of the class (Dog in this case).
PythonclassDog:species="Canine"# Class attributedef__init__(self,name,age):self.name=name# Instance attributeself.age=age# Instance attribute
Explanation:
- class Dog: Defines a class named Dog.
- species: A class attribute shared by all instances of the class.
- __init__ method: Initializes the name and age attributes when a new object is created.
Note: For more information, refer topython classes.
Python Objects
An Object is an instance of a Class. It represents a specific implementation of the class and holds its own data.
An object consists of:
- State: It is represented by the attributes and reflects the properties of an object.
- Behavior: It is represented by the methods of an object and reflects the response of an object to other objects.
- Identity: It gives a unique name to an object and enables one object to interact with other objects.
Creating Object
Creating an object in Python involves instantiating a class to create a new instance of that class. This process is also referred to as object instantiation.
PythonclassDog:species="Canine"# Class attributedef__init__(self,name,age):self.name=name# Instance attributeself.age=age# Instance attribute# Creating an object of the Dog classdog1=Dog("Buddy",3)print(dog1.name)print(dog1.species)
Explanation:
- dog1 = Dog("Buddy", 3): Creates an object of the Dog class with name as "Buddy" and age as 3.
- dog1.name:Accesses the instance attribute name of the dog1 object.
- dog1.species: Accesses the class attribute species of the dog1 object.
Note: For more information, refer topython objects.
Self Parameter
selfparameter is a reference to the current instance of the class. It allows us to access the attributes and methods of the object.
PythonclassDog:species="Canine"# Class attributedef__init__(self,name,age):self.name=name# Instance attributeself.age=age# Instance attributedog1=Dog("Buddy",3)# Create an instance of Dogdog2=Dog("Charlie",5)# Create another instance of Dogprint(dog1.name,dog1.age,dog1.species)# Access instance and class attributesprint(dog2.name,dog2.age,dog2.species)# Access instance and class attributesprint(Dog.species)# Access class attribute directly
OutputBuddy 3 CanineCharlie 5 CanineCanine
Explanation:
- self.name:Refers to the name attribute of the object (dog1) calling the method.
- dog1.bark():Calls the bark method on dog1.
Note: For more information, refer toself in the Python class
__init__ Method
__init__ method is the constructor in Python, automatically called when a new object is created. It initializes the attributes of the class.
PythonclassDog:def__init__(self,name,age):self.name=nameself.age=agedog1=Dog("Buddy",3)print(dog1.name)
Explanation:
- __init__:Special method used for initialization.
- self.name and self.age: Instance attributes initialized in the constructor.
Class and Instance Variables
In Python, variables defined in a class can be either class variables or instance variables, and understanding the distinction between them is crucial for object-oriented programming.
Class Variables
These are the variables that are shared across all instances of a class. It is defined at the class level, outside any methods. All objects of the class share the same value for a class variable unless explicitly overridden in an object.
Instance Variables
Variables that are unique to each instance (object) of a class. These are defined within the __init__ method or other instance methods. Each object maintains its own copy of instance variables, independent of other objects.
PythonclassDog:# Class variablespecies="Canine"def__init__(self,name,age):# Instance variablesself.name=nameself.age=age# Create objectsdog1=Dog("Buddy",3)dog2=Dog("Charlie",5)# Access class and instance variablesprint(dog1.species)# (Class variable)print(dog1.name)# (Instance variable)print(dog2.name)# (Instance variable)# Modify instance variablesdog1.name="Max"print(dog1.name)# (Updated instance variable)# Modify class variableDog.species="Feline"print(dog1.species)# (Updated class variable)print(dog2.species)
OutputCanineBuddyCharlieMaxFelineFeline
Explanation:
- Class Variable (species):Shared by all instances of the class. Changing Dog.species affects all objects, as it's a property of the class itself.
- Instance Variables (name, age): Defined in the __init__ method. Unique to each instance (e.g., dog1.name and dog2.name are different).
- Accessing Variables: Class variables can be accessed via the class name (Dog.species) or an object (dog1.species). Instance variables are accessed via the object (dog1.name).
- Updating Variables:Changing Dog.species affects all instances. Changing dog1.name only affects dog1 and does not impact dog2.
Python Inheritance
Inheritance allows a class (child class) to acquire properties and methods of another class (parent class). It supports hierarchical classification and promotes code reuse.
Types of Inheritance:
- Single Inheritance: A child class inherits from a single parent class.
- Multiple Inheritance:A child class inherits from more than one parent class.
- Multilevel Inheritance: A child class inherits from a parent class, which in turn inherits from another class.
- Hierarchical Inheritance: Multiple child classes inherit from a single parent class.
- Hybrid Inheritance: A combination of two or more types of inheritance.
Python# Single InheritanceclassDog:def__init__(self,name):self.name=namedefdisplay_name(self):print(f"Dog's Name:{self.name}")classLabrador(Dog):# Single Inheritancedefsound(self):print("Labrador woofs")# Multilevel InheritanceclassGuideDog(Labrador):# Multilevel Inheritancedefguide(self):print(f"{self.name}Guides the way!")# Multiple InheritanceclassFriendly:defgreet(self):print("Friendly!")classGoldenRetriever(Dog,Friendly):# Multiple Inheritancedefsound(self):print("Golden Retriever Barks")# Example Usagelab=Labrador("Buddy")lab.display_name()lab.sound()guide_dog=GuideDog("Max")guide_dog.display_name()guide_dog.guide()retriever=GoldenRetriever("Charlie")retriever.display_name()retriever.greet()retriever.sound()
Explanation:
- Single Inheritance:Labrador inherits Dog's attributes and methods.
- Multilevel Inheritance:GuideDog extends Labrador, inheriting both Dog and Labrador functionalities.
- Multiple Inheritance:GoldenRetriever inherits from both Dog and Friendly.
Note: For more information, refer to ourInheritance in Python tutorial.
Python Polymorphism
Polymorphism allows methods to have the same name but behave differently based on the object's context. It can be achieved through method overriding or overloading.
Types of Polymorphism
- Compile-Time Polymorphism: This type of polymorphism is determined during the compilation of the program. It allows methods or operators with the same name to behave differently based on their input parameters or usage. It is commonly referred to as method or operator overloading.
- Run-Time Polymorphism: This type of polymorphism is determined during the execution of the program. It occurs when a subclass provides a specific implementation for a method already defined in its parent class, commonly known as method overriding.
Code Example:
Python# Parent ClassclassDog:defsound(self):print("dog sound")# Default implementation# Run-Time Polymorphism: Method OverridingclassLabrador(Dog):defsound(self):print("Labrador woofs")# Overriding parent methodclassBeagle(Dog):defsound(self):print("Beagle Barks")# Overriding parent method# Compile-Time Polymorphism: Method Overloading MimicclassCalculator:defadd(self,a,b=0,c=0):returna+b+c# Supports multiple ways to call add()# Run-Time Polymorphismdogs=[Dog(),Labrador(),Beagle()]fordogindogs:dog.sound()# Calls the appropriate method based on the object type# Compile-Time Polymorphism (Mimicked using default arguments)calc=Calculator()print(calc.add(5,10))# Two argumentsprint(calc.add(5,10,15))# Three arguments
Explanation:
1. Run-Time Polymorphism:
- Demonstrated using method overriding in the Dog class and its subclasses (Labrador and Beagle).
- The correct sound method is invoked at runtime based on the actual type of the object in the list.
2. Compile-Time Polymorphism:
- Python does not natively support method overloading. Instead, we use a single method (add) with default arguments to handle varying numbers of parameters.
- Different behaviors (adding two or three numbers) are achieved based on how the method is called.
Note:For more information, refer to ourPolymorphism in Python Tutorial.
Python Encapsulation
Encapsulation is the bundling of data (attributes) and methods (functions) within a class, restricting access to some components to control interactions.
A class is an example of encapsulation as it encapsulates all the data that is member functions, variables, etc.

Types of Encapsulation:
- Public Members: Accessible from anywhere.
- Protected Members: Accessible within the class and its subclasses.
- Private Members: Accessible only within the class.
Code Example:
PythonclassDog:def__init__(self,name,breed,age):self.name=name# Public attributeself._breed=breed# Protected attributeself.__age=age# Private attribute# Public methoddefget_info(self):returnf"Name:{self.name}, Breed:{self._breed}, Age:{self.__age}"# Getter and Setter for private attributedefget_age(self):returnself.__agedefset_age(self,age):ifage>0:self.__age=ageelse:print("Invalid age!")# Example Usagedog=Dog("Buddy","Labrador",3)# Accessing public memberprint(dog.name)# Accessible# Accessing protected memberprint(dog._breed)# Accessible but discouraged outside the class# Accessing private member using getterprint(dog.get_age())# Modifying private member using setterdog.set_age(5)print(dog.get_info())
Explanation:
- Public Members:Easily accessible, such as name.
- Protected Members: Used with a single _, such as _breed. Access is discouraged but allowed in subclasses.
- Private Members:Used with __, such as __age. Access requiresgetter and setter methods.
Note:for more information, refer to ourEncapsulation in Python Tutorial.
Data Abstraction
Abstractionhides the internal implementation details while exposing only the necessary functionality. It helps focus on "what to do" rather than "how to do it."
Types of Abstraction:
- Partial Abstraction: Abstract class contains both abstract and concrete methods.
- Full Abstraction: Abstract class contains only abstract methods (like interfaces).
Code Example:
PythonfromabcimportABC,abstractmethodclassDog(ABC):# Abstract Classdef__init__(self,name):self.name=name@abstractmethoddefsound(self):# Abstract Methodpassdefdisplay_name(self):# Concrete Methodprint(f"Dog's Name:{self.name}")classLabrador(Dog):# Partial Abstractiondefsound(self):print("Labrador Woof!")classBeagle(Dog):# Partial Abstractiondefsound(self):print("Beagle Bark!")# Example Usagedogs=[Labrador("Buddy"),Beagle("Charlie")]fordogindogs:dog.display_name()# Calls concrete methoddog.sound()# Calls implemented abstract method
Explanation:
- Partial Abstraction: The
Dog
class has both abstract (sound
) and concrete (display_name
) methods. - Why Use It: Abstraction ensures consistency in derived classes by enforcing the implementation of abstract methods.
OOPs Quiz:
Related Articles:
Recommended Problems:

Introduction to Oops in Python

Python Classes and Objects

Class Instance Attributes in Python

Class Members Access in Python

Encapsulation in Python

Abstraction in Python

Abstract Class in Python

Inheritance in Python

Types of Inheritance in Python

Multiple Inheritance in Python

Polymorphism in Python

More Examples of Polymorphism in Python