Java OOP Introduction

Object-oriented programming, or OOP, is a way to organize a program around objects. In Java, an object combines data and behavior in one unit.

You have already used classes such as Main. In OOP, you also create your own classes to describe the things your program works with, such as a student, account, product, or game character.

Classes And Objects

A class is a blueprint. It describes what an object has and what an object can do. An object is a real value created from that class.

For example, a Car class can describe that every car has a brand and a speed. Each individual car object can then store its own brand and speed.

class Car {
    String brand;
    int speed;

    void drive() {
        System.out.println(brand + " is driving at " + speed + " mph");
    }
}

public class Main {
    public static void main(String[] args) {
        Car car = new Car();
        car.brand = "Toyota";
        car.speed = 45;

        car.drive();
    }
}

Output:

Toyota is driving at 45 mph

The variable car stores an object created from the Car class. The expression new Car() creates the object. The dot operator, such as car.brand and car.drive(), accesses fields and methods on that object.

Fields And Methods

A field is a variable that belongs to an object. A method is a block of code that belongs to an object or class.

In the previous example, brand and speed are fields. The drive method uses those fields to print a message. This is one of the main ideas of OOP: keep related data and actions together.

Multiple Objects

A class can create many objects. Each object has its own copy of the instance fields, so changing one object does not change the others.

class Player {
    String name;
    int score;

    void showScore() {
        System.out.println(name + ": " + score);
    }
}

public class Main {
    public static void main(String[] args) {
        Player first = new Player();
        first.name = "Ava";
        first.score = 10;

        Player second = new Player();
        second.name = "Ben";
        second.score = 7;

        first.showScore();
        second.showScore();
    }
}

Output:

Ava: 10
Ben: 7

Both first and second are Player objects. They were made from the same class, but they store different field values.

Constructors

A constructor is special code that runs when an object is created. It often gives fields their starting values.

class Book {
    String title;
    String author;

    Book(String bookTitle, String bookAuthor) {
        title = bookTitle;
        author = bookAuthor;
    }

    void describe() {
        System.out.println(title + " by " + author);
    }
}

public class Main {
    public static void main(String[] args) {
        Book book = new Book("Clean Code", "Robert Martin");
        book.describe();
    }
}

Output:

Clean Code by Robert Martin

The constructor has the same name as the class: Book. It does not have a return type. When new Book(...) runs, Java creates a Book object and passes the values into the constructor.

Why OOP Helps

  • OOP keeps related data and behavior in the same place.
  • Classes make programs easier to divide into smaller parts.
  • Objects let the same blueprint represent many separate values.
  • Methods on objects make code read more like the problem being solved.

Common OOP Words

Term Meaning
class A blueprint that describes fields and methods.
object A value created from a class.
field A variable that belongs to an object.
method A named action that belongs to an object or class.
constructor Code that runs when a new object is created.

Takeaway: Java OOP uses classes as blueprints and objects as working values that hold fields and call methods.