Java Object-Oriented Programming Concepts

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects,” which contain data and the functions to manipulate that data. This post provides an overview of the foundational principles of OOP in Java, suitable for beginners and intermediate programmers.

What Is Object-Oriented Programming?

Object-Oriented Programming allows you to create clear modular code, where you can use objects as building blocks. The main principles of OOP in Java include:

  1. Encapsulation: Wrapping data and methods into a single unit.
  2. Inheritance: Creating new classes that are built upon existing classes.
  3. Polymorphism: Allowing objects to be treated as instances of their parent class.
  4. Abstraction: Hiding the complex reality while exposing only necessary parts.

Let’s delve into these concepts.

Encapsulation

Encapsulation means bundling the data (fields) and methods (functions) together and restricting direct access to some of an object’s components. In Java, this can be done using private fields and public methods.

public class BankAccount {
    private double balance;

    public double getBalance() {
        return balance;
    }

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

Here, the balance attribute is kept private and can be accessed only through the public methods provided.

Inheritance

Inheritance allows a class to inherit attributes and methods from another class. It helps in code reusability and building relationships between different classes.

public class Vehicle {
    public void run() {
        System.out.println("Vehicle is running");
    }
}

public class Car extends Vehicle {
    public void fuelType() {
        System.out.println("Car uses petrol");
    }
}

Car myCar = new Car();
myCar.run(); // Outputs "Vehicle is running"

The Car class inherits the run method from Vehicle, so you don’t have to rewrite that method in Car.

Polymorphism

Polymorphism enables objects to be treated as instances of their parent class. It allows one interface to be used for a general class of actions.

public class Cat extends Animal {
    public void sound() {
        System.out.println("Cat meows");
    }
}

Animal myAnimal = new Cat();
myAnimal.sound(); // Outputs "Cat meows"

Here, the variable myAnimal is of type Animal, but it refers to an instance of Cat. When the sound method is called, the JVM figures out at runtime that it should call the Cat’s sound method.

Abstraction

Abstraction helps you focus on what an object does rather than how it does it. It’s a way of hiding the implementation details and showing only the functionality.

abstract class Appliance {
    abstract void turnOn();
    abstract void turnOff();
}

public class Fan extends Appliance {
    void turnOn() {
        System.out.println("Fan is on");
    }
    void turnOff() {
        System.out.println("Fan is off");
    }
}

An abstract class can have abstract methods (methods without a body) that must be implemented in any derived (non-abstract) class.

Conclusion

Object-Oriented Programming in Java helps in creating scalable, reusable, and maintainable code. Understanding the principles of Encapsulation, Inheritance, Polymorphism, and Abstraction is crucial for effective Java programming. These Object-Oriented Programming concepts enable developers to write cleaner, more reusable, and maintainable code.