Unit 3: Object Oriented Programming Using Python — Short Questions
12th Class Computer Science · Unit 3: Object Oriented Programming Using Python
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.
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.
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).
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.
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.
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.
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.
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.
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.
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.