Python OOP Tutorial (2026): Complete Guide to Object-Oriented Programming with Examples
What is Object-Oriented Programming (OOP)
What is a Class?
A class is a blueprint or template used to create
objects.
It defines the structure and behavior that every object
created from it will have.
A class itself does not store actual values. Instead, it
describes what properties and actions an object should contain.
Real-Life Example
Imagine an architect creating a blueprint for a house.
The blueprint describes:
- Number
of rooms
- Doors
- Windows
- Kitchen
The blueprint is the class.
The actual house built from that blueprint is an object.
Python Example
class Student:
pass
Here, Student is a class.
What is an Object?
An object is a real instance of a class.
Objects occupy memory and contain actual values.
A single class can create many different objects.
Real-Life Example
Suppose a school has a class named Student.
Students may include:
- Rahul
- Priya
- Sneha
- Amit
The class is Student, while Rahul, Priya, Sneha, and
Amit are different objects created from that class.
Python Example
class Student:
pass
student1 = Student()
student2 = Student()
Two different objects are created from the same class.
python exception Handling Tutorial
What is a Constructor?
A constructor is a special method that automatically
runs whenever an object is created.
In Python, the constructor is written using the __init__()
method.
It initializes object data without requiring a separate
function call.
Why is a Constructor Important?
Without a constructor, we would need to assign values
manually after creating every object.
The constructor saves time by assigning initial values
automatically.
Example
class Student:
self.name = name
self.age = age
student = Student("Aisha", 20)
print(student.name)
print(student.age)
Output
Aisha
20
What are Instance Variables?
Instance variables are variables that belong to an object.
Every object has its own separate copy of these variables.
Changing the value in one object does not affect another
object.
Example
class Employee:
def __init__(self,
name, salary):
self.name =
name
self.salary =
salary
emp1 = Employee("Ravi", 30000)
emp2 = Employee("Priya", 45000)
print(emp1.salary)
print(emp2.salary)
Output
30000
45000
Both employees store different salaries because each object
has its own instance variables.
What are Methods?
Methods are functions defined inside a class.
They describe what an object can do.
Real-Life Example
A smartphone can:
- Make
calls
- Send
messages
- Open
apps
- Take
photos
These actions are similar to methods.
Example
class Car:
print("Car Started")
car = Car()
car.start()
Output
Car Started
What is Inheritance?
Inheritance allows one class to acquire the properties and
methods of another class.
Instead of writing the same code repeatedly, we can reuse an
existing class.
Real-Life Example
A child inherits many qualities from parents.
Similarly, one class can inherit features from another
class.
Example
class Animal:
print("Animal makes sound")
class Dog(Animal):
pass
dog.speak()
Output
Animal makes sound
The Dog class automatically gets the speak() method from the
Animal class.
What is Polymorphism?
The word Polymorphism means many forms.
The same method name can perform different actions depending
on the object that uses it.
Real-Life Example
The word Run has different meanings.
- A
person runs.
- A
machine runs.
- Software
runs.
The word is the same, but the behavior changes.
Example
class Bird:
def sound(self):
print("Bird Chirps")
class Dog:
def sound(self):
print("Dog Barks")
bird = Bird()
dog = Dog()
bird.sound()
dog.sound()
Output
Bird Chirps
Dog Barks
The method name is the same, but the output differs.
What is Encapsulation?
Encapsulation means combining data and methods into a single
unit while protecting important data from direct access.
Instead of allowing anyone to modify sensitive information,
access is controlled through methods.
Real-Life Example
Consider an ATM.
You cannot directly access the money inside the machine.
You interact with it througah secure operations like
inserting a card, entering a PIN, and choosing a transaction.
The internal data remains protected.
Example
class BankAccount:
def
__init__(self):
self.__balance
= 10000
def
show_balance(self):
print(self.__balance)
account = BankAccount()
account.show_balance()
Output
10000
The balance is protected from direct access outside the
class.
What is Abstraction?
Abstraction means showing only the essential features while
hiding unnecessary implementation details.
Users only need to know how to use something, not how it
works internally.
Real-Life Example
When you drive a car, you use the steering wheel,
accelerator, and brake pedals.
You do not need to understand how the engine, gearbox, or
fuel injection system works.
The complex details remain hidden.
Example
from abc import ABC, abstractmethod
class Shape(ABC):
@abstractmethod
def area(self):
pass
class Rectangle(Shape):
def area(self):
print("Area = Length × Width")
rectangle = Rectangle()
rectangle.area()
Output
Area = Length × Width
The abstract class defines what every shape should do, while
each specific shape provides its own implementation.
Summary of OOP Concepts
|
Concept |
Purpose |
|
Object-Oriented Programming |
Organizes software using objects |
|
Class |
Blueprint for creating objects |
|
Object |
Real instance of a class |
|
Constructor |
Initializes object data automatically |
|
Instance Variables |
Store data unique to each object |
|
Methods |
Define the behavior of objects |
|
Inheritance |
Reuses code from another class |
|
Polymorphism |
One interface with multiple behaviors |
|
Encapsulation |
Protects data by controlling access |
|
Abstraction |
Hides implementation details and exposes only necessary
functionality |
Python OOP Interview Questions and Answers
1. What is Object-Oriented Programming (OOP)?
Object-Oriented Programming (OOP) is a programming
approach that organizes code into objects. An object contains both data
(attributes) and behavior (methods). OOP helps create software that is modular,
reusable, and easier to maintain.
2. What is a Class in Python?
A class is a blueprint or template used to create objects. It defines the properties (attributes) and behaviors (methods) that objects of that class will have.
3. What is an Object?
An object is an instance of a class. It contains actual values for the
attributes defined in the class and can call the methods of that class.
4. What is a Constructor in Python?
A constructor is a special method named __init__() that is automatically called
when an object is created. It initializes the object's attributes.
5. What is the purpose of the self keyword?
The self keyword refers to the current object. It allows access to the object's
instance variables and methods within the class.
6. What are Instance Variables?
Instance variables are variables that belong to an individual object. Each
object has its own copy of these variables.
7. What are Methods in Python?
Methods are functions defined inside a class. They describe the actions or
behaviors that an object can perform.
8. What is Inheritance?
Inheritance is an OOP feature that allows one class (child class) to inherit
the properties and methods of another class (parent class). It promotes code
reuse.
9. What is Polymorphism?
Polymorphism means "many forms." It allows the same method name or
interface to perform different actions depending on the object using it.
10. What is Encapsulation?
Encapsulation is the process of combining data and methods into a single unit
while restricting direct access to sensitive data.
11. What is Abstraction?
Abstraction is the process of hiding implementation details and showing only
the essential functionality to the user.
12. What are the four pillars of OOP?
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
13. Can one class create multiple objects?
Yes. A single class can be used to create any number of objects, and each
object has its own data.
14. Why is OOP important?
OOP improves code organization, reusability, scalability, security, and makes
large applications easier to develop and maintain.
Frequently Asked Questions (FAQs)
1. What is Object-Oriented Programming in Python?
Object-Oriented Programming (OOP) is a programming
paradigm that uses classes and objects to organize code and model real-world
entities.
2. Why should I learn OOP in Python?
Learning OOP helps you write cleaner, reusable, and
maintainable code. It is widely used in web development, desktop applications,
games, automation, and enterprise software.
3. What is the difference between a class and an object?
A class is a blueprint, while an object is an actual
instance created from that blueprint.
4. What is the purpose of the __init__() method?
The __init__() method initializes an object by assigning
values to its attributes when the object is created.
5. What is the difference between instance variables and
class variables?
- Instance
variables belong to individual objects and can have different values.
- Class
variables are shared among all objects of the class.
6. What is inheritance used for?
Inheritance is used to reuse existing code by allowing a
child class to inherit the features of a parent class.
7. What is the benefit of encapsulation?
Encapsulation protects data from unauthorized access and
helps maintain the integrity of an object's state.
8. What is polymorphism in simple words?
Polymorphism allows the same method name to behave
differently for different objects, making code more flexible.
9. What is abstraction with a real-life example?
Abstraction hides complex implementation details and
exposes only necessary functionality. For example, when driving a car, you use
the steering wheel and pedals without needing to know how the engine works
internally.
10. Is Object-Oriented Programming required for Python
interviews?
Yes. OOP is one of the most commonly asked topics in
Python technical interviews. Questions about classes, objects, inheritance,
polymorphism, encapsulation, abstraction, and constructors are frequently
included in interviews for beginner and experienced Python developers alike.
Conclusion
Object-Oriented Programming is one of the most important concepts in Python because it helps developers create software that is organized, reusable, secure, and easy to maintain. Understanding classes, objects, constructors, instance variables, methods, inheritance, polymorphism, encapsulation, and abstraction provides a strong foundation for building everything from simple applications to large enterprise systems. Mastering these concepts will also help you perform better in technical interviews and write cleaner, more professional Python code.
Comments
Post a Comment