What is OOP?
Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around data, or objects, rather than functions and logic. An object is an entity that has state (fields) and behaviour (methods).
OOP was designed to overcome the limitations of Procedure-Oriented Programming (POP). In POP, data and functions are separate and data can be accessed by any function โ making large programs difficult to manage and prone to errors.
| POP (Procedure-Oriented) | OOP (Object-Oriented) |
|---|---|
| Program divided into functions | Program divided into objects |
| Data moves freely between functions | Data is hidden inside objects (encapsulation) |
| Top-down approach | Bottom-up approach |
| Less secure โ global data | More secure โ data hiding |
| Examples: C, Pascal, COBOL | Examples: Java, C++, Python |
Object-Oriented Paradigm
The OOP paradigm is built on four main pillars:
- Encapsulation โ bundling data and methods together; hiding internal details
- Abstraction โ showing only essential features; hiding complexity
- Inheritance โ acquiring properties from a parent class
- Polymorphism โ one interface, many forms
Objects and Classes
Class
A class is a blueprint or template for creating objects. It defines the data (fields) and behaviour (methods) that its objects will have. A class does not occupy memory by itself.
Object
An object is an instance of a class โ a real-world entity created from the class blueprint. Each object has its own copy of instance variables and shares class methods.
class Student {
// Fields (state)
int rollNo;
String name;
double marks;
// Method (behaviour)
void display() {
System.out.println("Roll: " + rollNo + " Name: " + name);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student(); // object creation
s1.rollNo = 101;
s1.name = "Arjun";
s1.marks = 92.5;
s1.display();
}
}new keyword.Encapsulation
Encapsulation is the mechanism of wrapping data (variables) and code (methods) together as a single unit. It protects data from direct access by outside code by making fields private and providing public getter/setter methods.
class BankAccount {
private double balance; // hidden from outside
public void deposit(double amount) {
if (amount > 0) balance += amount;
}
public double getBalance() {
return balance; // controlled access
}
}Abstraction
Abstraction means hiding the implementation details and showing only the functionality to the user. It reduces complexity and allows the programmer to focus on what an object does instead of how it does it.
In Java, abstraction is achieved through:
- Abstract classes (0% to 100% abstraction)
- Interfaces (100% abstraction in older Java)
Inheritance
Inheritance is the mechanism by which one class acquires the properties (fields) and behaviours (methods) of another class. It promotes code reusability.
- The class that is inherited from is called the Parent / Super / Base class
- The class that inherits is called the Child / Sub / Derived class
class Animal {
void eat() { System.out.println("Animal eats"); }
}
class Dog extends Animal { // inherits Animal
void bark() { System.out.println("Dog barks"); }
}
Dog d = new Dog();
d.eat(); // inherited method
d.bark(); // own methodPolymorphism
Polymorphism means "many forms". It allows a single action to behave differently based on the object it is acting upon.
Types of Polymorphism in Java
| Type | Also Called | Resolved At | Achieved By |
|---|---|---|---|
| Compile-time | Static / Early Binding | Compile time | Method Overloading |
| Runtime | Dynamic / Late Binding | Runtime | Method Overriding |
// Compile-time polymorphism (Overloading)
class Calculator {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
}Dynamic Binding
Dynamic Binding (or Late Binding) means that the method to be called is determined at runtime, not compile time. This is the basis of runtime polymorphism in Java.
When a parent class reference holds a child class object, and an overridden method is called, Java uses the actual object type (not the reference type) to decide which method to call.
class Bank {
double getRate() { return 0; }
}
class SBI extends Bank {
double getRate() { return 7.0; }
}
class HDFC extends Bank {
double getRate() { return 6.5; }
}
Bank b = new SBI(); // parent ref โ child object
System.out.println(b.getRate()); // prints 7.0 โ decided at runtime
b = new HDFC();
System.out.println(b.getRate()); // prints 6.5Advantages of OOP
- Modularity โ Each object is self-contained; easy to locate and fix bugs
- Reusability โ Classes can be reused in different programs via inheritance
- Security โ Data encapsulation hides internal details from the outside
- Flexibility โ Polymorphism makes code adaptable to different situations
- Maintainability โ OOP code is easier to maintain and upgrade
- Real-world modeling โ Objects map naturally to real-world entities
Download PDF Notes
Save these notes as a PDF for offline reference