Definition:

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects“, which can contain data (in the form of fields or attributes) and methods (functions) that act on the data. Java is a pure object-oriented language where everything is modeled as an object. OOP allows the creation of reusable and modular code that models real-world entities.

In Java, the four main principles of OOP are:

  1. Encapsulation
  2. Inheritance
  3. Polymorphism
  4. Abstraction

Example:

// Demonstrating OOP Principles

// 1. Encapsulation: Class with private fields and public getters/setters
class Person {
    private String name;
    private int age;

    // Constructor
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Getters and Setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    // Method
    public void introduce() {
        System.out.println("Hello, my name is " + name + " and I am " + age + " years old.");
    }
}

// 2. Inheritance and Polymorphism
class Student extends Person {
    private String course;

    public Student(String name, int age, String course) {
        super(name, age); // Calling the parent class constructor
        this.course = course;
    }

    // Overriding the introduce method
    @Override
    public void introduce() {
        System.out.println("Hi, I am " + getName() + ", a " + course + " student, and I am " + getAge() + " years old.");
    }
}

// 3. Abstraction
interface Vehicle {
    void start();
    void stop();
}

class Car implements Vehicle {
    @Override
    public void start() {
        System.out.println("Car is starting...");
    }

    @Override
    public void stop() {
        System.out.println("Car is stopping...");
    }
}

public class OOPExample {
    public static void main(String[] args) {
        // Encapsulation
        Person person = new Person("Alice", 30);
        person.introduce();

        // Inheritance and Polymorphism
        Student student = new Student("Bob", 20, "Computer Science");
        student.introduce();

        // Abstraction
        Vehicle car = new Car();
        car.start();
        car.stop();
    }
}

Output:

Hello, my name is Alice and I am 30 years old.
Hi, I am Bob, a Computer Science student, and I am 20 years old.
Car is starting...
Car is stopping...

Features of OOP in Java:

1.Encapsulation:

  • Bundling the data (fields) and methods (functions) that operate on the data into a single unit, i.e., a class. It also hides the internal state of objects from outside interference.

2.Inheritance:

  • Mechanism where one class (child class) inherits properties and behaviors from another class (parent class), allowing code reuse.

3.Polymorphism:

  • The ability of a method to behave differently based on the object calling it. There are two types: compile-time (method overloading) and runtime (method overriding).

4.Abstraction:

  • Hiding implementation details and only exposing the essential features of an object. This is achieved in Java through abstract classes and interfaces.

Advantages of OOP in Java:

  • Each object is self-contained, which makes it easy to troubleshoot and manage complex systems.
  • Through inheritance, objects can be reused across different programs or parts of a program.
  • OOP makes it easier to update or extend systems as it isolates changes within specific objects.
  • The modular approach and code reusability lead to faster development cycles and easier debugging.
  • OOP allows developers to model real-world entities and interactions in a natural and intuitive way, improving the clarity and intuitiveness of code.

Uses of OOP in Java:

  • Java’s Swing and JavaFX libraries use OOP concepts to create interactive, event-driven interfaces.
  • OOP is widely used in large-scale systems like enterprise resource planning (ERP), customer relationship management (CRM), and other business applications due to its modular structure.
  • Game entities like characters, weapons, and environments can be modeled as objects with behaviors, making OOP ideal for game development.
  • OOP concepts are used in Android development (Java or Kotlin) to create reusable components like activities, fragments, and services.
  • Java-based web frameworks like Spring and JavaServer Faces (JSF) heavily utilize OOP to build scalable and maintainable web applications.