Object-Oriented Programming (OOP) is one of the most important concepts in programming. It is widely used in languages such as Java, Python, C++, and C# to develop software applications.
Many beginners learn programming using variables, loops, functions, and conditional statements. As applications become larger and more complex, organizing code becomes increasingly important. This is where Object-Oriented Programming helps.
OOP organizes programs using objects and classes. It allows developers to create reusable, maintainable, and structured code.
The four main pillars of Object-Oriented Programming are:
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
In this article, we will understand OOP, its important concepts, and the four pillars with simple real-life examples and programming examples.
What Is Object-Oriented Programming (OOP)?
Object-Oriented Programming is a programming paradigm that organizes software around objects rather than only functions and procedures.
An object represents a real-world or conceptual entity. It contains:
- Data: Attributes or properties of an object.
- Methods: Functions that define the behavior of an object.
For example, consider a car.
A car has properties such as:
- Color
- Brand
- Model
- Speed
It also has behaviors such as:
- Start
- Stop
- Accelerate
- Brake
In OOP, these properties and behaviors can be represented using a class and its objects.
Definition of OOP
Object-Oriented Programming is a programming approach based on classes and objects that combines data and methods into a single unit to develop modular and reusable software.
What Are Classes and Objects in OOP?
Classes and objects are the fundamental building blocks of Object-Oriented Programming.
What Is a Class?
A class is a blueprint or template used to create objects. It defines the attributes and methods that its objects will have.
Real-Life Example:
A house blueprint defines the structure of a house, including rooms, doors, and windows. However, the blueprint itself is not an actual house.
Similarly, a class defines the structure of objects.
What Is an Object?
An object is an instance of a class. It represents an actual entity created using the class.
For example, if Car is a class, then a BMW car, Toyota car, and Honda car can be objects of that class.
Class and Object Example in Java
class Car {
String brand;
String color;
void start() {
System.out.println("Car is starting");
}
}
public class Main {
public static void main(String[] args) {
Car c1 = new Car();
c1.brand = "Toyota";
c1.color = "Red";
System.out.println(c1.brand);
System.out.println(c1.color);
c1.start();
}
}
Output:
Toyota
Red
Car is starting
Four Pillars of Object-Oriented Programming
The four pillars of OOP are the core principles that make Object-Oriented Programming powerful and useful.
| Pillar | Meaning | Real-Life Example |
|---|---|---|
| Encapsulation | Bundling data and methods together while controlling access | ATM machine |
| Inheritance | Acquiring properties and methods from another class | Child inheriting traits from parents |
| Polymorphism | One interface or method name having different behaviors | A person performing different roles |
| Abstraction | Hiding implementation details and showing essential features | Driving a car |
1. Encapsulation
What Is Encapsulation?
Encapsulation is the process of combining data and methods into a single unit and restricting direct access to the internal data.
In Java, encapsulation is commonly implemented using private variables and public getter and setter methods.
Real-Life Example of Encapsulation
Consider an ATM machine.
When a user withdraws money from an ATM, they cannot directly access the bank’s internal account database. Instead, they interact with the ATM interface.
The ATM controls how the account data is accessed and modified.
This is an example of encapsulation because the internal data is protected and access is controlled.
Java Example of Encapsulation
class BankAccount {
private double balance;
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.deposit(5000);
System.out.println(account.getBalance());
}
}
Output:
5000.0
Explanation
balanceis declared as a private variable.- It cannot be accessed directly outside the class.
- The
deposit()method controls how the balance is updated. - The
getBalance()method provides controlled access to the balance.
Advantages of Encapsulation
- Protects data from unauthorized direct access.
- Improves data security.
- Makes code easier to maintain.
- Provides controlled access to variables.
- Reduces unwanted changes to data.
2. Inheritance
What Is Inheritance?
Inheritance is an OOP concept in which one class acquires the properties and methods of another class.
The class that provides properties and methods is called the parent class or superclass.
The class that inherits those properties and methods is called the child class or subclass.
Inheritance promotes code reusability.
Real-Life Example of Inheritance
Consider a family.
A child may inherit certain characteristics from their parents, such as eye color or other traits.
Similarly, in programming, a child class can inherit properties and methods from a parent class.
Java Example of Inheritance
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
Output:
Animal is eating
Dog is barking
Explanation
Animalis the parent class.Dogis the child class.- The
Dogclass inherits theeat()method fromAnimal. - The
Dogclass also has its ownbark()method.
The extends keyword is used to implement inheritance in Java.
Types of Inheritance
| Type | Description |
|---|---|
| Single Inheritance | One child class inherits from one parent class |
| Multilevel Inheritance | A class inherits from a class that already inherits from another class |
| Hierarchical Inheritance | Multiple child classes inherit from one parent class |
| Multiple Inheritance | One child class inherits from multiple parent classes |
| Hybrid Inheritance | Combination of two or more inheritance types |
Note: Java does not support multiple inheritance of classes directly. However, it supports multiple inheritance of type through interfaces.
Advantages of Inheritance
- Promotes code reusability.
- Reduces code duplication.
- Makes programs easier to maintain.
- Supports hierarchical relationships between classes.
3. Polymorphism
What Is Polymorphism?
Polymorphism means “many forms.”
In Object-Oriented Programming, polymorphism allows the same method name or interface to represent different behaviors depending on the context.
Polymorphism helps developers write flexible and reusable code.
Real-Life Example of Polymorphism
Consider a person who can perform different roles.
A person can act as:
- A student in college.
- An employee in an organization.
- A customer in a shopping mall.
The same person behaves differently depending on the situation.
Similarly, a programming method can behave differently based on its arguments or the object invoking it.
Types of Polymorphism
There are two main types of polymorphism in Java:
- Compile-Time Polymorphism
- Runtime Polymorphism
Compile-Time Polymorphism
Compile-time polymorphism is achieved through method overloading.
Method overloading occurs when multiple methods in the same class have the same name but different parameter lists.
Java Example
class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
public class Main {
public static void main(String[] args) {
Calculator c = new Calculator();
System.out.println(c.add(10, 20));
System.out.println(c.add(10, 20, 30));
}
}
Output:
30
60
The add() method performs different operations depending on the number of arguments.
Runtime Polymorphism
Runtime polymorphism is achieved through method overriding.
Method overriding occurs when a child class provides its own implementation of a method inherited from the parent class.
Java Example
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
@Override
void sound() {
System.out.println("Cat meows");
}
}
public class Main {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
a1.sound();
a2.sound();
}
}
Output:
Dog barks
Cat meows
Explanation
Animalis the parent class.DogandCatoverride thesound()method.- The same method call produces different outputs.
- The actual object determines which implementation is executed.
Advantages of Polymorphism
- Improves code flexibility.
- Supports method overriding and overloading.
- Makes code easier to extend.
- Reduces code duplication.
- Helps implement dynamic behavior.
4. Abstraction
What Is Abstraction?
Abstraction is the process of hiding implementation details and showing only the essential features of an object.
It focuses on what an object does rather than how it does it.
Abstraction reduces complexity and makes applications easier to use.
Real-Life Example of Abstraction
Consider driving a car.
A driver uses:
- Steering wheel
- Accelerator
- Brake
- Gear system
The driver does not need to understand the complete internal working of the engine to drive the car.
The car provides essential controls while hiding the internal implementation.
This is an example of abstraction.
Abstraction in Java
Java supports abstraction using:
- Abstract classes
- Interfaces
Abstract Class Example
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car starts using a key");
}
}
public class Main {
public static void main(String[] args) {
Vehicle v = new Car();
v.start();
}
}
Output:
Car starts using a key
Explanation
Vehicleis an abstract class.- The
start()method is declared without an implementation. - The
Carclass provides the implementation. - The user interacts with the vehicle through its essential behavior.
Advantages of Abstraction
- Hides unnecessary implementation details.
- Reduces program complexity.
- Improves code maintainability.
- Provides a clear structure.
- Supports flexible application design.
Difference Between the Four Pillars of OOP
| Concept | Main Purpose | Key Mechanism | Example |
|---|---|---|---|
| Encapsulation | Protect data | Access modifiers | Bank account |
| Inheritance | Reuse code | extends keyword | Dog inherits Animal |
| Polymorphism | Different behaviors | Overloading and overriding | Animal sounds |
| Abstraction | Hide implementation | Abstract classes and interfaces | Car driving |
Object-Oriented Programming is an essential programming paradigm used to develop structured, reusable, and maintainable software applications.
The four pillars of OOP provide the foundation for understanding how objects interact and how software can be designed efficiently.
- Encapsulation protects data and controls access.
- Inheritance promotes code reusability.
- Polymorphism allows different behaviors through common interfaces.
- Abstraction hides implementation details.
For B.Tech students and beginners, understanding these concepts is important for learning Java, C++, Python, Data Structures and Algorithms, and preparing for technical interviews.
Start practicing OOP concepts with simple programs such as Student Management System, Bank Account Management System, Library Management System, and Employee Management System.
Explore more placement preparation resources, interview questions, aptitude practice, engineering notes, and career guides on Quescol.
Visit Quescol →