TExcelTAD
Home › Java › Java OOP

Java OOP

Java is an object-oriented programming (OOP) language. So far you have written code as a sequence of steps inside main and some static methods — the procedural style. OOP takes a different view: a program is a collection of objects that each hold some data and know how to do things with that data.

Classes and objects

  • A class is a blueprint. It describes what data its objects have (fields) and what they can do (methods).
  • An object is a concrete thing built from that blueprint. Each object has its own copy of the data.
Class (blueprint) Objects (instances)
Car a red Toyota, a blue Volvo, a black Tesla
BankAccount Ada's account, Linus's account
Song "Bohemian Rhapsody", "Yesterday"

Here is a tiny taste — a Dog class and two dog objects:

java · try it yourself
class Dog {
    String name;
    int age;

    void bark() {
        System.out.println(name + " says woof!");
    }
}

class Main {
    public static void main(String[] args) {
        Dog rex = new Dog();
        rex.name = "Rex";
        rex.age = 3;

        Dog bella = new Dog();
        bella.name = "Bella";
        bella.age = 5;

        rex.bark();
        bella.bark();
        System.out.println(rex.name + " is " + rex.age + ", " + bella.name + " is " + bella.age);
    }
}
Rex says woof!
Bella says woof!
Rex is 3, Bella is 5

The keyword new creates an object. Each Dog has its own name and age, and bark() uses the data of the dog it was called on.

Procedural vs. object-oriented

Compare two ways to work with a bank account. Procedurally, data and functions are separate and you pass the data around:

java
double balance = 100;
balance = deposit(balance, 50);

With OOP, the data and the behaviour live together, and the object protects its own state:

java · try it yourself
class Account {
    private double balance;

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

    double getBalance() {
        return balance;
    }
}

class Main {
    public static void main(String[] args) {
        Account acc = new Account();
        acc.deposit(100);
        acc.deposit(50);
        acc.deposit(-999);          // rejected by the object itself
        System.out.println("Balance: " + acc.getBalance());
    }
}
Balance: 150.0

The four pillars of OOP

Pillar Idea Chapter
Encapsulation hide internal data; expose a controlled interface Encapsulation
Inheritance build a new class on top of an existing one Inheritance
Polymorphism one interface, many implementations Polymorphism
Abstraction focus on what an object does, not how Abstraction, Interfaces

You don't need to master these yet — the next chapters introduce them one at a time.

Why OOP?

  • Organisation — code about a concept lives in one place (the class).
  • Reuse — write a class once, create as many objects as you need, and extend it via inheritance.
  • Safety — objects can validate changes to their own data.
  • Modelling — classes map naturally to things in the problem domain: customers, orders, invoices.

Tip: Follow the DRY principle — Don't Repeat Yourself. When you notice the same group of variables and logic appearing in several places, it's often a sign that a class is waiting to be created.

Everything is (almost) an object

In Java, strings, lists, files and threads are all objects created from classes in the standard library. Even System.out is an object (of class PrintStream), and println is one of its methods. Only the eight primitive types (int, double, ...) are not objects.

java · try it yourself
class Main {
    public static void main(String[] args) {
        String text = new String("objects everywhere");
        StringBuilder sb = new StringBuilder("abc");
        sb.append("def");
        System.out.println(text.toUpperCase());
        System.out.println(sb.length());
        System.out.println(System.out.getClass().getSimpleName());
    }
}
OBJECTS EVERYWHERE
6
PrintStream

Test yourself

What is the relationship between a class and an object?

  1. They are the same thing
  2. A class is a blueprint; an object is an instance of it
  3. An object is a blueprint for classes
  4. A class is a method inside an object

Which keyword creates a new object?

  1. class
  2. create
  3. new
  4. object

Which is NOT one of the four pillars of OOP?

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