csaccept.com is a computer awareness website dedicated to providing reliable and easy-to-understand information about computer technology and digital safety. The website focuses on educating students, beginners, and general users about computer basics, cyber security, emerging technologies, and practical IT skills. Through informative articles, quizzes, and real-life examples, csaccept.com aims to increase digital literacy and help users stay safe and confident in today’s technology-driven world.
 Facebook   ………………..      Instagram   ……………..      Twitter ( X )      ……………..     YouTube


Object-Oriented Programming (OOP) in Python :

1. Introduction to OOP in Python

In Python, Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects, which are instances of classes. OOP focuses on encapsulating data and behavior together within objects. It improves code reusability, modularity, and extensibility.

Python fully supports OOP concepts such as:

  • Classes & Objects
  • Inheritance
  • Polymorphism
  • Encapsulation
  • Abstraction

2. Classes and Objects in Python

A class is a blueprint or template for creating objects. It defines the properties (attributes) and behaviors (methods) of objects.
An object is an instance of a class.

✅ Example: Class and Object in Python

class Car:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def drive(self):
        print("The car is driving.")

# Creating objects
car1 = Car("Toyota", "Camry")
car2 = Car("Honda", "Accord")

# Accessing attributes
print(car1.brand)

# Calling method
car1.drive()

3. Constructors and Destructors in Python

A constructor is a special method that is automatically called when an object is created. It initializes object attributes.
Constructor name in Python: __init__()

A destructor is called when an object is about to be destroyed or garbage collected.
Destructor name in Python: __del__()

Example:

class Student:
    def __init__(self, name):
        self.name = name
        print("Constructor called")

    def __del__(self):
        print("Destructor called")

s = Student("Lav")

4. Inheritance and Polymorphism in Python

Inheritance is a mechanism in OOP that allows a class to inherit properties and method from another class. The class that is inherited from is called Superclass or parent class, and the class that inherits is called the subclass or child class. In Python, a subclass can override or extend the functionality of the superclass.

Inheritance allows one class to inherit properties and methods from another class.

  • Parent Class → Superclass
  • Child Class → Subclass

Polymorphism means “many forms”. It allows objects of different classes to be treated as objects of a common superclass.
Polymorphism is the ability of an object to take on many forms. It allows objects of different classes to be treated as objects of a common superclass.

Example:

class Animal:
    def sound(self):
        print("Animal makes sound")

class Dog(Animal):
    def sound(self):
        print("Dog barks")

d = Dog()
d.sound()

5. Encapsulation and Data Hiding in Python

Encapsulation is the bundling of data (attributes) and methods (functions) within a class. It provides a way to organize code and data into logical units, promoting code maintainability and reusability. Data hiding is a specific aspect of encapsulation where class attributes are kept private, restricting direct access from outside the class. This promotes information hiding and data integrity.


Encapsulation
is the bundling of data and methods inside a class.
Data hiding restricts direct access to class variables by making them private.

Example:

class BankAccount:
    def __init__(self, acc_no):
        self.__acc_no = acc_no   # private variable

    def get_acc_no(self):
        return self.__acc_no

account = BankAccount("23456")

print(account.get_acc_no())   # Correct way
# print(account.__acc_no)     # ❌ Error (private)

6. Method Overriding and Overloading in python

Method overriding occurs when a subclass provides its own implementation of a method inherited from the superclass. Method overloading involves defining multiple methods with the same name but different parameters within a class.


Method Overriding
occurs when a subclass provides its own implementation of a method from the parent class.

Method Overloading means multiple methods with the same name but different parameters (Python handles it using default arguments).

Example:

class Shape:
    def area(self):
        print("Calculating area")

class Rectangle(Shape):
    def area(self, length=0, width=0):
        if length and width:
            print("Rectangle area:", length * width)
        else:
            super().area()

r = Rectangle()
r.area(4, 5)

Key Points (Exam Ready)

  • OOP organizes programs using classes and objects
  • __init__() → Constructor | __del__() → Destructor
  • Inheritance promotes code reuse
  • Polymorphism → Same method, different behavior
  • Encapsulation → Data protection using private variables
  • Overriding → Redefining parent method in child class

Short Questions & Answers (OOP in Python)


Q1. What is OOP?
OOP (Object-Oriented Programming) is a programming paradigm that organizes code into objects and classes.

Q2. What is a class?
A class is a blueprint for creating objects.

Q3. What is an object?
An object is an instance of a class.

Q4. What is encapsulation?
Encapsulation is bundling data and methods within a class.

Q5. What is data hiding?
Restricting direct access to class variables using private variables.

Q6. What is inheritance?
Inheritance allows one class to acquire properties of another class.

Q7. What is polymorphism?
Polymorphism allows one method to behave differently in different situations.

Q8. What is constructor?
A special method __init__() used to initialize objects.

Q9. What is destructor?
A special method __del__() called when object is destroyed.

Q10. What is method overriding?
Redefining a parent class method in child class.

Q11. What is method overloading?
Using same method name with different parameters.

Q12. What is self in Python?
It refers to current object of the class.

Q13. What is superclass?
Parent class from which properties are inherited.

Q14. What is subclass?
Child class that inherits from parent class.

Q15. What is abstraction?
Hiding implementation details and showing only functionality.

Q16. What are attributes?
Variables inside a class.

Q17. What are methods?
Functions inside a class.

Q18. Can Python support multiple inheritance?
Yes.

Q19. How to make private variable?
Using double underscore __var.

Q20. What is object creation syntax?
obj = ClassName()


MCQ Quiz (60 Questions)

Basic OOP

1. OOP stands for?
A) Object Oriented Programming ✅
B) Only Object Program
C) Order of Programming
D) None

2. A class is a?
A) Object
B) Blueprint ✅
C) Variable
D) Function

3. Object is instance of?
A) Function
B) Class ✅
C) Method
D) Loop

4. Which keyword defines class?
A) def
B) class ✅
C) object
D) create

5. Constructor name?
A) init()
B) init() ✅
C) start()
D) create()


Encapsulation

6. Encapsulation means?
A) Binding data & methods ✅
B) Looping
C) Printing
D) None

7. Private variable prefix?
A) _
B) __ ✅
C) ###
D) &

8. Data hiding improves?
A) Speed
B) Security ✅
C) Color
D) Size


Inheritance

9. Inheritance promotes?
A) Code reuse ✅
B) Delay
C) Memory loss
D) None

10. Parent class called?
A) Subclass
B) Superclass ✅
C) Object
D) Method

11. Child class called?
A) Superclass
B) Subclass ✅
C) Loop
D) Variable

12. Python supports multiple inheritance?
A) Yes ✅
B) No


Polymorphism

13. Polymorphism means?
A) One form
B) Many forms ✅
C) No form
D) Print form

14. Method overriding occurs in?
A) Same class
B) Child class ✅
C) Function
D) Loop


Constructor & Destructor

15. Destructor name?
A) del()
B) del() ✅
C) destroy()
D) end()

16. Constructor runs when?
A) Object created ✅
B) Program ends
C) Loop runs
D) None


Methods & Objects

17. self refers to?
A) Class
B) Object itself ✅
C) Function
D) Loop

18. Method is?
A) Function inside class ✅
B) Variable
C) Object
D) Loop

19. Attribute is?
A) Class variable ✅
B) Function
C) Loop
D) None


True Concept

20. OOP improves modularity?
A) Yes ✅
B) No

21. OOP improves reusability?
A) Yes ✅
B) No

22. Can we access private variable directly?
A) Yes
B) No ✅


Code Based

23. Object creation syntax?
A) obj = Class() ✅
B) Class = obj()
C) new obj()
D) create obj()

24. Calling method syntax?
A) obj.method() ✅
B) method(obj)
C) call method
D) None


Advanced OOP

25. Abstraction hides?
A) Data
B) Implementation ✅
C) Object
D) Loop

26. Overloading uses?
A) Same method name ✅
B) Different class
C) Loop
D) None

27. Overriding changes?
A) Parent method ✅
B) Loop
C) Variable
D) None

28. Class variable shared by?
A) All objects ✅
B) One object
C) Loop
D) None

29. Instance variable belongs to?
A) Object ✅
B) Class
C) Loop
D) None

30. Which is OOP language?
A) Python ✅
B) C
C) Assembly
D) HTML


More MCQs (31–60)

  1. __init__ is special method → Constructor ✅
  2. __del__ is → Destructor ✅
  3. Encapsulation provides → Security ✅
  4. Inheritance type → Single, Multiple, Multilevel ✅
  5. Python is → Object oriented ✅
  6. Private variable example → __x
  7. Protected variable → _x
  8. Method overriding runtime → Yes ✅
  9. OOP main goal → Reusability ✅
  10. Class keyword → class ✅
  11. Object stores → Data & behavior ✅
  12. Polymorphism supports → Same interface ✅
  13. Constructor returns → None ✅
  14. Method belongs to → Class ✅
  15. Instance created using → Class name ✅
  16. Encapsulation binds → Data + Method ✅
  17. Parent → Superclass ✅
  18. Child → Subclass ✅
  19. Overloading Python → Default args used ✅
  20. Multiple inheritance allowed → Yes ✅
  21. Class inside class → Nested class ✅
  22. Object memory allocated → Runtime ✅
  23. Access private via → Getter method ✅
  24. self mandatory → Yes ✅
  25. OOP reduces → Code duplication ✅
  26. Class method decorator → @classmethod
  27. Static method decorator → @staticmethod
  28. Method overriding example → Child redefining parent ✅
  29. Encapsulation symbol → __
  30. Python supports OOP fully → Yes ✅