Object-Oriented Programming (OOP)
5. Object-Oriented Programming (OOP)
a. Classes and Objects
Object‑oriented programming organizes code around objects, which combine data and behavior. A class is like a blueprint or template that defines what data (attributes) and actions (methods) an object will have. An object is a specific instance created from that class.
For example, a “Car” class might define attributes like color and speed, and methods like start and stop. Each actual car in your program would be an object built from that class, with its own specific values for those attributes.
This way of thinking helps you model real‑world entities and keep related data and functions together. It can make large programs more modular, easier to reason about, and easier to extend.
b. Constructors
A constructor is a special method that runs automatically when an object is created. It is responsible for setting up the initial state of the object, such as assigning default values or processing arguments passed during creation.
By using constructors, you ensure that every object starts in a valid, predictable state. This reduces the chances of bugs due to uninitialized data and makes object creation more expressive and explicit.
Constructors also provide a convenient place to enforce rules, such as requiring certain parameters or validating input before an object is fully created.
c. Inheritance
Inheritance allows one class to build on another by reusing and extending its behavior. A child (or subclass) inherits attributes and methods from a parent (or base class). It can then add new features or override existing ones.
For example, if you have a general “Vehicle” class, you can create more specific classes like “Car” or “Bike” that inherit from it. They can reuse the common behavior of vehicles while adding their own specifics.
Inheritance promotes code reuse and helps you express “is‑a” relationships in your design. However, it should be used thoughtfully, as deep or complex hierarchies can become hard to maintain.
d. Polymorphism
Polymorphism means “many forms,” and in OOP it refers to the ability to use a single interface to represent different underlying forms (classes). In practice, this allows you to call the same method name on different objects, and each object can respond in its own way.
For instance, if both a “Circle” and “Rectangle” class implement a “draw” method, you can call draw on a collection of shapes without knowing which specific type each one is. Each object will execute its own version of the method.
Polymorphism makes code more flexible and extendable. New classes can be introduced without changing existing code that relies on the common interface, which is very powerful in larger systems.
e. Encapsulation and Abstraction
Encapsulation is the practice of bundling data and methods inside a class and controlling access to them. Some details are kept private, while a public interface exposes only what other parts of the program need to use. This protects internal state from accidental misuse and supports safer changes later.
Abstraction means focusing on what an object does rather than how it does it. You define clear interfaces and hide complex implementation details. Users of the class do not need to know the internal workings; they just need to know how to interact with it.
Together, encapsulation and abstraction help reduce complexity. They encourage clean boundaries between components and make it easier to reason about parts of the system independently.










