Unit 3: Object Oriented Programming Using Python — Short Questions

12th Class Computer Science · Unit 3: Object Oriented Programming Using Python

Short1.What is Object-Oriented Programming (OOP)?

Object-Oriented Programming (OOP) is a method of designing programs using objects and classes, where each object represents a real-world entity with specific properties (attributes) and behaviors (methods). Classes act as templates for creating multiple similar objects. OOP is built on the core concepts of encapsulation, inheritance, and polymorphism, which together help model real-world problems efficiently and produce well-structured, maintainable programs.

Short2.What is the purpose of a class in Python?

A class in Python serves as a blueprint or template that defines the properties (attributes) and actions (methods) of an entity. Its purpose is to organize data and related functionality together so that multiple objects with similar characteristics can be created from it, making programs more structured and manageable.

Short3.What is the difference between a class and an object?

A class is a blueprint or template that defines the attributes and methods an entity will have, but it does not hold actual data by itself. An object is an instance of a class that holds actual values for those attributes and can use the class's methods. One class can be used to create many different objects, each with its own independent data (e.g., object1 = Student("Ali") and object2 = Student("Ahmed") are two different objects of the same Student class).

Short4.What is purpose of __init__...?

The _init_ method is a special constructor method in a Python class that is automatically called when a new object of the class is created. Its purpose is to initialize the object's attributes with starting values, for example 'self.name = name' sets the name attribute for a newly created Student object.

Short5.What is encapsulation in OOP, and why is it important?

Encapsulation is the OOP concept of bundling data (variables) and methods (functions) into a single unit, the class, and restricting direct access to that data. It is important because it protects data from unwanted access, improves security, makes code easier to manage, and allows modification of data only in a controlled manner through defined methods.

Short6.How does encapsulation help in keeping data secure and private?

Encapsulation keeps data secure by marking attributes as private using a double underscore prefix (e.g., self._balance), which Python converts internally through name mangling so the attribute cannot be accessed directly from outside the class. Access and modification are only possible through the class's own public methods, such as deposit(), withdraw(), and getbalance(), which can include checks (like verifying amount <= balance) before changing the data, keeping it private and protected from accidental or unauthorized changes.

Short7.What is inheritance in OOP, and how does it help in reusing functionality?

Inheritance is an OOP concept that allows a class (subclass/child class) to take the properties and methods of another class (superclass/parent class). It helps reuse functionality because the child class automatically gets all the attributes and methods of the parent class without rewriting them, and it can add new features or override existing ones, reducing redundancy and promoting code reuse.

Short8.How does inheritance enable the extension of functionality in Python?

Inheritance enables extension of functionality because a child class can add its own new methods and attributes in addition to what it inherits (for example, Dog(Animal) inherits eat() from Animal and adds its own bark() method). A child class can also override a parent's method to provide its own specific implementation, using super() to still call the parent's version when needed, allowing programs to be extended without rewriting existing code.

Short9.What is polymorphism in OOP, and how does it make code more flexible?

Polymorphism means "many forms"; it is the ability of an entity, such as a method or object, to perform different actions depending on the context. It makes code more flexible because the same method name (for example, processPayment() or sound()) can be called on different objects and produce different, object-specific behavior, without the calling code needing to know the exact type of object in advance.

Short10.How does polymorphism help in creating adaptable and reusable code?

Through runtime polymorphism (method overriding), different classes such as Dog, Cat, and Animal can each define their own version of a common method like sound(). A single piece of code, such as a loop calling animal.sound() on a list of mixed objects, automatically calls the correct version based on the object's actual class at runtime. This means new classes can be added later without changing the existing calling code, making the program adaptable and reusable.