Tutorials Home   >   Programming Basics   >   Introduction to Object-Oriented Programming

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:

  1. Encapsulation – Combining data and methods in a single unit (class) and restricting direct access to some of the object’s components.

  2. Inheritance – Creating new classes from existing ones, allowing reuse of code.

  3. Polymorphism – Objects can take many forms, allowing the same operation to behave differently on different classes.

  4. 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:

class Car:
def __init__(self, brand, model, year):
self.brand = brand
self.model = model
self.year = year
def start_engine(self):
print(f”{self.brand} {self.model}‘s engine started!”)

Here:

  • Car is a class.

  • brand, model, and year are 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.

my_car = Car("Toyota", "Corolla", 2020)
my_car.start_engine()

Output:

Toyota Corolla's engine started!

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.

class BankAccount:
def __init__(self, balance):
self.__balance = balance # private attribute
def deposit(self, amount):
self.__balance += amount

def get_balance(self):
return self.__balance

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.

class Vehicle:
def __init__(self, brand):
self.brand = brand
def move(self):
print(f”{self.brand} is moving.”)

class Bike(Vehicle):
def ring_bell(self):
print(“Ring Ring!”)

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.

class Dog:
def sound(self):
print("Woof!")
class Cat:
def sound(self):
print(“Meow!”)

animals = [Dog(), Cat()]
for animal in animals:
animal.sound()

Output:

Woof!
Meow!

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.

from abc import ABC, abstractmethod

class Shape(ABC):
@abstractmethod
def area(self):
pass

class Square(Shape):
def __init__(self, side):
self.side = side

def area(self):
return self.side * self.side

s = Square(4)
print(s.area())

Here, the user only interacts with Square and doesn’t need to know the internal details of Shape.


4. Advantages of OOP

  1. Reusability – Code can be reused through inheritance.

  2. Modularity – Programs are divided into classes and objects, making them easier to manage.

  3. Maintainability – Encapsulation and abstraction make programs easier to maintain.

  4. Scalability – OOP makes it simpler to extend programs with new features.

  5. 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#.