Introduction to Object-Oriented Programming
1. What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects rather than functions and logic. Objects are instances of classes, which can contain both data (known as attributes or properties) and behavior (known as methods or functions).
OOP helps programmers model real-world entities in code. For example, consider a program for managing a library. In a traditional approach, you might have separate functions and data for books, users, and transactions. In OOP, you can create objects like Book, User, and Library, each with their own attributes and behaviors. This approach makes programs easier to understand, maintain, and extend.
Key Features of OOP:
-
Encapsulation – Combining data and methods in a single unit (class) and restricting direct access to some of the object’s components.
-
Inheritance – Creating new classes from existing ones, allowing reuse of code.
-
Polymorphism – Objects can take many forms, allowing the same operation to behave differently on different classes.
-
Abstraction – Hiding complex implementation details and showing only necessary features.
2. Classes and Objects
Classes
A class is like a blueprint for creating objects. It defines the structure and behavior that the objects will have. For example:
Here:
-
Caris a class. -
brand,model, andyearare attributes. -
start_engine()is a method.
Objects
An object is an instance of a class. It represents a specific entity that follows the blueprint of the class.
Output:
Here, my_car is an object of the class Car. Each object can have different attribute values but share the same behavior.
3. Four Pillars of OOP
a) Encapsulation
Encapsulation restricts direct access to some of an object’s components, which helps prevent accidental modification. This is often done using private variables and getter/setter methods.
Here, __balance is private and can only be accessed through the class’s methods.
b) Inheritance
Inheritance allows one class (child/subclass) to inherit properties and behaviors from another class (parent/superclass). This promotes code reuse.
Bike inherits from Vehicle, so it can use the move() method.
c) Polymorphism
Polymorphism allows objects of different classes to respond to the same method in different ways.
Output:
Even though the method sound() is called in the same way, each object responds differently.
d) Abstraction
Abstraction hides unnecessary details from the user and shows only what is important.
Here, the user only interacts with Square and doesn’t need to know the internal details of Shape.
4. Advantages of OOP
-
Reusability – Code can be reused through inheritance.
-
Modularity – Programs are divided into classes and objects, making them easier to manage.
-
Maintainability – Encapsulation and abstraction make programs easier to maintain.
-
Scalability – OOP makes it simpler to extend programs with new features.
-
Real-world modeling – Objects can model real-world entities more naturally than functions alone.
5. Common OOP Terminology
| Term | Meaning |
|---|---|
| Class | Blueprint for objects |
| Object | Instance of a class |
| Method | Function defined inside a class |
| Attribute | Variable defined inside a class |
| Constructor | Special method to initialize objects (e.g., __init__ in Python) |
| Inheritance | Mechanism to create a new class from an existing class |
| Polymorphism | Ability of different objects to respond differently to the same method |
| Encapsulation | Hiding data inside objects and controlling access |
| Abstraction | Hiding complex implementation details |
6. Summary
Object-Oriented Programming is a powerful way to structure programs using objects and classes. By combining data and behavior in a single entity, OOP makes programming closer to real-world modeling. The four pillars—Encapsulation, Inheritance, Polymorphism, and Abstraction—help developers write clean, reusable, and maintainable code. Understanding OOP is essential for modern programming, especially in languages like Python, Java, C++, and C#.