โ† Back to Courses
ยฉ 2025 sampathirao yoganandh
Week 1 & 2 ยท CO-1

OOP Fundamentals

Object-Oriented Paradigm, Objects & Classes, Encapsulation, Inheritance, Polymorphism, Dynamic Binding  CO-1

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 functionsProgram divided into objects
Data moves freely between functionsData is hidden inside objects (encapsulation)
Top-down approachBottom-up approach
Less secure โ€” global dataMore secure โ€” data hiding
Examples: C, Pascal, COBOLExamples: Java, C++, Python

Object-Oriented Paradigm

The OOP paradigm is built on four main pillars:

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();
    }
}
๐Ÿ“Œ Key PointA class is a logical entity; an object is a physical entity. Memory is allocated only when an object is created using the 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
    }
}
๐Ÿ“Œ Key PointEncapsulation = Data Hiding + Data Binding. It increases security and maintains integrity of data.

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:

๐Ÿ’ก Did You KnowExample: When you press the accelerator in a car, you don't need to know the internal combustion process โ€” you just use the interface. That's abstraction.

Inheritance

Inheritance is the mechanism by which one class acquires the properties (fields) and behaviours (methods) of another class. It promotes code reusability.

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 method

Polymorphism

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

TypeAlso CalledResolved AtAchieved By
Compile-timeStatic / Early BindingCompile timeMethod Overloading
RuntimeDynamic / Late BindingRuntimeMethod 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.5
๐Ÿ“Œ Key PointDynamic binding enables runtime polymorphism. The JVM checks the actual object type at runtime and calls the appropriate overridden method.

Advantages of OOP

๐Ÿ“„

Download PDF Notes

Save these notes as a PDF for offline reference

View / Download PDF