Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java OOP Introduction
Object Oriented Programming



Object-Oriented Programming (OOP) is not just a way of writing code—it's a philosophy. It’s how we model the real world inside our programs. Java is one of the most popular languages to embrace OOP from the ground up. If you're new to Java, understanding its object-oriented foundation is your first real step into writing clean, scalable, and maintainable software.

What is Object-Oriented Programming?

OOP is a programming paradigm centered around the concept of objects—instances of classes—that interact with one another. Instead of thinking in terms of functions and logic alone, OOP encourages thinking in terms of entities, their attributes, and their behaviors.

Why Java Uses OOP

Java uses OOP to:

Core Concepts of Java OOP

Let’s dive into the four foundational pillars of Java OOP:

1. Class and Object

A class is like a blueprint. An object is a real-world instance of that blueprint.

class Car {
    String color;
    void drive() {
        System.out.println("The car is driving.");
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();       // object creation
        myCar.color = "Red";
        myCar.drive();               // method call
        System.out.println(myCar.color);
    }
}
The car is driving.
Red

Explanation:

Here, Car is the class with a property and a method. myCar is an object created from that class. It accesses the method and variable defined in the class.

2. Encapsulation

Encapsulation means bundling data and methods that work on that data into a single unit (class), and restricting access to some components.

class BankAccount {
    private double balance = 1000.0;

    public double getBalance() {
        return balance;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }
}
public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount();
        System.out.println("Initial Balance: " + account.getBalance());
        account.deposit(500);
        System.out.println("Updated Balance: " + account.getBalance());
    }
}
Initial Balance: 1000.0
Updated Balance: 1500.0

Explanation:

The balance variable is private, meaning it cannot be accessed directly. This ensures data protection. Access is controlled using public methods.

3. Inheritance

Inheritance allows a class to inherit properties and methods from another class. This promotes code reuse and logical hierarchy.

class Animal {
    void makeSound() {
        System.out.println("Some generic sound");
    }
}

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

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.makeSound(); // inherited method
        d.bark();      // specific method
    }
}
Some generic sound
Dog barks

Explanation:

The Dog class extends Animal. It gains access to the makeSound() method and also adds its own behavior with bark().

4. Polymorphism

Polymorphism allows us to perform a single action in different ways. Java supports two types:

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

    double add(double a, double b) {
        return a + b;
    }
}
public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(5, 3));        // int version
        System.out.println(calc.add(5.2, 3.8));    // double version
    }
}
8
9.0

Explanation:

The method add is overloaded with different parameter types. This is polymorphism in action—same name, different behavior.

5. Abstraction

Abstraction hides unnecessary details and shows only the essential features of an object.

abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing a circle");
    }
}

public class Main {
    public static void main(String[] args) {
        Shape s = new Circle();  // abstract class reference
        s.draw();
    }
}
Drawing a circle

Explanation:

The abstract class Shape defines an abstract method draw(). The Circle class implements that method, completing the abstraction cycle.

Final Thoughts

Java OOP is the backbone of writing meaningful Java applications. As you move forward, remember: classes are blueprints, objects are real, and encapsulation, inheritance, polymorphism, and abstraction are the four wheels that keep your code vehicle moving smoothly.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M