Lesson 37 of 50 – Java Object-Oriented Programming (OOP)
74%

Java Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming approach based on classes and objects. Java uses OOP concepts to organize code, represent real-world entities, and build reusable and maintainable applications.

1. What Is OOP in Java?

OOP stands for Object-Oriented Programming. It organizes programs around objects that combine data and behavior.

For example, a Student object can contain:

  • Data: name, age, course
  • Behavior: study, display details
class Student {
    String name = "Rahul";
    int age = 20;

    void display() {
        System.out.println(name);
        System.out.println(age);
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        s1.display();
    }
}
Output:
Rahul
20

The class defines the structure, and the object stores its own instance data and uses its methods.

2. Main Concepts of OOP

The four commonly taught pillars of OOP are:

Concept Meaning
Encapsulation Combining data and methods while controlling access to data.
Inheritance Creating a class based on another class.
Polymorphism Allowing the same operation or interface to have different behaviors.
Abstraction Exposing essential features while hiding implementation details.
These concepts help developers design programs that are easier to understand, extend, and maintain.
3. Class and Object

A class is a blueprint. An object is an instance created from that class.

class Car {
    String brand;
    String color;

    void display() {
        System.out.println(brand + " " + color);
    }

    public static void main(String[] args) {
        Car c1 = new Car();
        Car c2 = new Car();

        c1.brand = "Toyota";
        c1.color = "White";

        c2.brand = "Honda";
        c2.color = "Black";

        c1.display();
        c2.display();
    }
}
Output:
Toyota White
Honda Black

Both objects belong to the same class but contain different instance field values.

4. Encapsulation

Encapsulation combines data and methods within a class. A common approach is to make fields private and provide public methods to access or update them safely.

class BankAccount {
    private double balance;

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    public double getBalance() {
        return balance;
    }

    public static void main(String[] args) {
        BankAccount account = new BankAccount();

        account.deposit(1000);

        System.out.println(account.getBalance());
    }
}
Output:
1000.0

The private field cannot be accessed directly from outside the class. The public methods control how its value is used.

5. Inheritance

Inheritance allows a subclass to extend a superclass and reuse its accessible members.

class Animal {
    void eat() {
        System.out.println("Animal is eating");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog is barking");
    }

    public static void main(String[] args) {
        Dog d1 = new Dog();

        d1.eat();
        d1.bark();
    }
}
Output:
Animal is eating
Dog is barking

The Dog class extends Animal, so a Dog object can use the inherited eat() method.

6. Polymorphism

Polymorphism means "many forms." In Java, a method call can produce different behavior depending on the applicable method overload or the runtime object.

Example: Runtime polymorphism

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");
    }
}

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

The method implementation is selected according to the actual object at runtime.

7. Abstraction

Abstraction focuses on what an object does rather than exposing all the details of how it works. Java supports abstraction through abstract classes and interfaces.

abstract class Shape {
    abstract double calculateArea();
}

class Rectangle extends Shape {
    double length;
    double width;

    Rectangle(double length, double width) {
        this.length = length;
        this.width = width;
    }

    @Override
    double calculateArea() {
        return length * width;
    }
}

class Main {
    public static void main(String[] args) {
        Shape s1 = new Rectangle(10, 5);

        System.out.println(s1.calculateArea());
    }
}
Output:
50.0

The Shape abstraction defines an operation, while Rectangle provides the implementation.

8. Constructors in OOP

Constructors initialize objects when they are created. They can assign initial values to an object's fields.

class Student {
    String name;
    int age;

    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    void display() {
        System.out.println(name + " " + age);
    }

    public static void main(String[] args) {
        Student s1 = new Student("Rahul", 20);
        s1.display();
    }
}
Output:
Rahul 20

The constructor receives values and initializes the new Student object.

9. Method Overloading

Method overloading allows multiple methods in the same class to have the same name but different parameter lists.

class Calculator {

    int add(int a, int b) {
        return a + b;
    }

    int add(int a, int b, int c) {
        return a + b + c;
    }

    public static void main(String[] args) {
        Calculator c1 = new Calculator();

        System.out.println(c1.add(10, 20));
        System.out.println(c1.add(10, 20, 30));
    }
}
Output:
30
60

The compiler selects the appropriate overloaded method based on the arguments.

10. Method Overriding

Method overriding occurs when a subclass provides its own implementation of an inherited instance method.

class Parent {
    void display() {
        System.out.println("Parent class");
    }
}

class Child extends Parent {
    @Override
    void display() {
        System.out.println("Child class");
    }

    public static void main(String[] args) {
        Parent p1 = new Child();
        p1.display();
    }
}
Output:
Child class

The @Override annotation helps the compiler verify that the method correctly overrides an inherited method.

11. Advantages of OOP
  • Reusability: Classes can reuse functionality through inheritance and composition.
  • Maintainability: Code can be organized into understandable classes.
  • Encapsulation: Access to internal data can be controlled.
  • Flexibility: Polymorphism allows different implementations behind a common interface.
  • Abstraction: Users can work with essential operations without knowing every detail.
OOP is especially useful for larger applications with many related entities and behaviors.
12. Real-Life Example: Employee Management
class Employee {
    private String name;
    private double salary;

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public void displayDetails() {
        System.out.println("Name: " + name);
        System.out.println("Salary: " + salary);
    }
}

class Manager extends Employee {
    private String department;

    Manager(String name, double salary, String department) {
        super(name, salary);
        this.department = department;
    }

    @Override
    public void displayDetails() {
        super.displayDetails();
        System.out.println("Department: " + department);
    }
}

class Main {
    public static void main(String[] args) {
        Manager m1 = new Manager(
            "Amit", 50000, "IT"
        );

        m1.displayDetails();
    }
}
Output:
Name: Amit
Salary: 50000.0
Department: IT

This example demonstrates encapsulation through private fields, inheritance through extends, constructor chaining through super(), and method overriding.

13. Practice Assignments
  1. Create a Student class with name, age, and a method to display student details.
  2. Demonstrate encapsulation using private fields and public getter methods.
  3. Create Animal and Dog classes to demonstrate inheritance.
  4. Create a Shape class hierarchy to demonstrate runtime polymorphism.
  5. Create an abstract class named Vehicle with an abstract method named start().
  6. Create overloaded add() methods that accept different numbers of integers.
  7. Create Parent and Child classes to demonstrate method overriding.
  8. Build a simple employee management program using multiple OOP concepts.
Practice Tip: Try implementing each OOP concept separately before combining them into a larger program.

🧠 Quick Quiz

1. What does OOP stand for?




2. Which OOP concept combines data and methods and can control access to data?




3. Which keyword is used to inherit a class in Java?