
Introduction to Object-Oriented Programming (OOP)
Object-Oriented Programming, or OOP, is a way of writing programs by focusing on real-life objects. In the real world, everything we deal with is an object, such as a car, a book, or a student. Each of these objects has certain features and can perform specific actions. In OOP, we try to represent these features as attributes and the actions as functions. The detailed tutorial can be visited here.
This programming style helps us organize our code in a way that is closer to how we understand things in real life. It makes large programs easier to design, manage, and extend
Aspect | Procedural Programming | Object-Oriented Programming (OOP) |
---|---|---|
Approach | Top-down | Bottom-up |
Structure | Program is divided into functions | Program is divided into objects and classes |
Data & Functions | Data and functions are separate | Data and functions are bundled together in objects |
Reusability | Limited | High (through inheritance and modularity) |
Security | Less secure (data is exposed) | More secure (data hidden using encapsulation) |
Real-world Modeling | Difficult to model real-world scenarios | Easier to model real-world entities and interactions |
Examples | C, Pascal | Java, C++, Python (with OOP), C# |
Function Reuse | Uses functions repeatedly | Uses objects and methods repeatedly |
Code Maintenance | Harder to manage and scale in large programs | Easier to manage due to modular structure |
Main Focus | Focuses on functions (actions) | Focuses on objects (entities) |
What is OOP?
Contents
Object-oriented programming is a programming model that allows developers to organize and build programs around real-world concepts. In this model, we define objects that contain both data (attributes) and actions (functions or methods).
The main concepts of OOP are:
- Classes
- Objects
- Attributes
- Functions
- Encapsulation
- Inheritance
- Polymorphism
Each of these will be explained step-by-step below.

Why Use Object-Oriented Programming (OOP)?
OOP offers several significant advantages that make it a popular programming paradigm:
Reusability: One of the core benefits of OOP is code reusability. Through inheritance, new classes can be created based on existing ones, inheriting their properties and methods. This means that you can reuse code from previously developed classes, saving time and effort.
Modularity: OOP promotes modularity by breaking down complex problems into smaller, more manageable objects. Each object encapsulates its data and behavior, making it easier to understand, modify, and test independently. This modular approach improves code organization and maintainability.
Maintainability: OOP code is generally easier to maintain than procedural code. The encapsulation of data and behavior within objects helps to isolate changes, making it less likely that modifications in one part of the code will have unintended consequences in another. This reduces the risk of introducing bugs and makes it easier to update and extend the software over time.
Flexibility: Polymorphism is a fundamental concept in OOP that allows objects of different classes to be treated as if they were of the same type. This provides flexibility and adaptability in your code. For example, you can create a function that takes a generic object as an argument, and it can work with objects of different classes as long as they have the necessary methods.
Key Concepts in Object-Oriented Programming (OOP)

Class is the template, design or blueprint for creating real-world objects. It defines what attributes and functions an object will have, but it doesn’t store real data itself.
Objects are instances of a class. They possess specific attributes (properties) and behaviors (methods). Each object has a unique identity, allowing it to be distinguished from others. For example my car is an object of the class car.
Encapsulation is the practice of hiding the internal implementation details of an object. This is achieved through access control mechanisms that determine how other parts of the program can interact with an object’s properties and methods. Encapsulation promotes modularity and maintainability by making objects more self-contained and easier to understand and modify. In example fo the car, the controls hides the inner functinalities of the car.
Inheritance is a mechanism that allows you to create new classes based on existing ones. The new class has an “is-a” relationship with the parent class, meaning it is a specialized version of the parent class. Inheritance promotes code reuse by allowing you to avoid duplicating code. For examole, animal is a base class and we create its subclass ‘cow’, which will inherit the functionalities of inheritance.
Class
A class is like a blueprint or design for creating objects. It defines what attributes and functions an object will have, but it doesn’t store real data itself.

For example, a Student
class can define:
- Attributes like
name
,rollNo
, anddepartment
- Functions like
giveExam()
andsubmitAssignment()
You can then create many student objects from this one class.
Object
An object is a real instance of a class. It is created using the blueprint defined by the class and stores actual data.

For example, once you define a Student
class, you can create:
Student s1 = new Student();
s1.name = "Ali";
s1.giveExam();
Here, s1
is an object of class Student
. Each object can have different values for the same attributes, like different names or roll numbers.
Attributes
Attributes are the properties or characteristics of an object. These are also called data members. They describe what an object has.

Attributes are written inside a class and accessed through objects.
For example:
class Book {
String title;
String author;
int pages;
}
Here, title
, author
, and pages
are attributes. Each book object will have its own values for these.
Functions
Functions, also called methods, define the actions an object can perform. These are written inside the class and can be called using the object.

For example:
class Book {
void read() {
System.out.println("Reading the book...");
}
void bookmarkPage() {
System.out.println("Page bookmarked.");
}
}
When we create a Book
object, it can call these functions to perform tasks.
Encapsulation
Encapsulation means protecting the internal data of an object by keeping attributes private and accessing them only through special functions (getters and setters).

This improves security and keeps the data safe from direct changes.
Example:
class Student {
private int marks;
public int getMarks() {
return marks;
}
public void setMarks(int m) {
marks = m;
}
}
Here, marks
is private, so it cannot be accessed directly.
Inheritance
Inheritance means creating a new class based on an existing class. The new class (called child) can use everything from the old class (called parent) and also add its own features.

Example:
class Vehicle {
void start() {
System.out.println("Starting...");
}
}
class Car extends Vehicle {
void playMusic() {
System.out.println("Playing music...");
}
}
Car
is using start()
from Vehicle
and also has its own function playMusic()
.
Polymorphism
Polymorphism means one function behaves differently for different classes. It allows the same function name to do different jobs based on the object.

Example:
class Animal {
void speak() {
System.out.println("Animal speaks");
}
}
class Dog extends Animal {
void speak() {
System.out.println("Dog barks");
}
}
When we call speak()
, the result depends on whether the object is an Animal or Dog.
Task
Take a Book as an example. Now apply OOP concepts:
- Object: A specific book like
book1
- Attributes: title, author, price
- Functions: read(), bookmarkPage()
- Encapsulation: Keep
price
private, use methods to get/set it - Inheritance: Make
EBook
a child class ofBook
- Polymorphism: Override
read()
inEBook
andPrintedBook