What is Object-Oriented Programming (OOP) in Java?

2 minute read

Object-Oriented Programming (OOP) is a way of writing programs by modeling them around objects — things that have data and behavior, just like objects in the real world.


The Real-World Analogy

Think about a car:

  • Data (attributes): color, brand, speed, fuel level
  • Behavior (actions): start, accelerate, brake, turn

In OOP, we model this car as an object. The blueprint for creating car objects is called a class.

class Car {
    // Data (fields)
    String brand;
    String color;
    int speed;

    // Behavior (methods)
    void accelerate() {
        speed += 10;
    }

    void brake() {
        speed -= 10;
    }
}
Car myCar = new Car();    // create an object from the blueprint
myCar.brand = "Toyota";
myCar.accelerate();       // call behavior on the object

The Core Idea

OOP is built on one simple principle:

Combine data and the functions that operate on that data into a single unit (an object).

Instead of having separate variables and functions floating around, you bundle them together:

Procedural Style OOP Style
Data and functions are separate Data and methods live together in objects
Functions act on external data Objects act on their own data
Hard to manage as programs grow Scales well to large programs

Four Pillars of OOP

Every OOP language, including Java, is built on four principles:

1. Encapsulation

Hide internal details, expose only what’s necessary.

// You don't need to know HOW the engine works to drive a car
myCar.accelerate();  // simple interface, complex internals

2. Inheritance

Create new classes based on existing ones — reuse code.

class ElectricCar extends Car {
    int batteryLevel;
}

3. Polymorphism

Same method name, different behavior depending on the object.

animal.makeSound();  // Dog barks, Cat meows — same method, different result

4. Abstraction

Focus on what an object does, not how it does it.

// You know a List can add and remove items
// You don't care if it's an ArrayList or LinkedList internally

We’ll explore each pillar in depth in the coming chapters.


Why Is Java an OOP Language?

In Java, everything revolves around classes and objects:

  • Every Java file must contain a class
  • The main method itself lives inside a class
  • Even primitive types have object wrappers (Integer, Double)
  • The entire standard library is organized as classes
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, OOP!");
    }
}

Even this simple program uses OOP — System is a class, out is an object, and println is a method.


OOP vs Other Paradigms

Paradigm Focus Example Languages
Procedural Procedures/functions C, Pascal
Object-Oriented Objects with data + behavior Java, C++, C#
Functional Pure functions, immutability Haskell, Erlang
Multi-paradigm Mix of all Python, Kotlin, Scala

Java is primarily OOP but has added functional features (lambdas, streams) since Java 8.


Quick Exercises

  1. Think of 3 real-world objects. For each, list 3 attributes and 3 behaviors.
  2. Write a simple Dog class with fields name, breed, age and methods bark(), eat(), sleep().
  3. In your own words, explain the difference between a class and an object.

Summary

  • OOP models programs around objects (data + behavior)
  • A class is a blueprint; an object is an instance of that blueprint
  • Four pillars: Encapsulation, Inheritance, Polymorphism, Abstraction
  • Java is fundamentally an OOP language

What’s Next?

Now let’s understand the concrete Benefits of OOP — why the industry chose this approach.

Updated: