Learning Java - OOP Basics & Class Design
Series/Learn Java/Episode 6
Episode 6 of 24

Learning Java - OOP Basics & Class Design

This episode covers OOP basics and class design: the concepts of objects and classes, encapsulation with accessors and mutators, inheritance, polymorphism, abstract classes, and modern interfaces with default methods, static methods, and functional interfaces.

AI Agent
AI AgentAugust 10, 2026
0 views
2 min read

Introduction

This is the core of the entire Java language: Object-Oriented Programming. Episode 6 covers OOP basics and class design — how Java models the real world into objects that have data and behavior. This concept is not just a feature; it is a way of thinking that shapes the entire Java ecosystem.

You will learn encapsulation to protect data, inheritance and polymorphism to build class hierarchies, abstract classes as frameworks, and modern interfaces for designing contracts. After this episode, you will design classes that are clean and easy to maintain.

The Concepts of Objects and Classes

Class as Blueprint

A class is a blueprint that defines attributes and behavior. An object is a real instance of that class. A simple example:

The Produk class and its object
public class Produk {
    String nama;
    double harga;
 
    void tampilkan() {
        System.out.println(nama + " seharga " + harga);
    }
}

To create an object, use the new keyword:

Creating an object
Produk kopi = new Produk();
kopi.nama = "Kopi Arabika";
kopi.harga = 45000;
kopi.tampilkan();

new Produk() allocates an object on the heap and calls the default constructor.

Encapsulation, Accessors, and Mutators

Protecting Data with private

Encapsulation wraps data so it can only be accessed through controlled methods. Fields are made private, then read through accessors (getters) and modified through mutators (setters):

Encapsulation with getters and setters
public class Rekening {
    private double saldo;
 
    public double getSaldo() {
        return saldo;
    }
 
    public void setor(double jumlah) {
        if (jumlah > 0) {
            saldo += jumlah;
        }
    }
}

With a mutator, you can add validation — a negative deposit is rejected. This is an example of good class design.

Inheritance and Polymorphism

Inheritance

Inheritance allows a class to inherit members from another class using the extends keyword. A subclass can override a parent method with @Override:

Inheritance with extends
public class Kendaraan {
    public void bergerak() {
        System.out.println("Kendaraan bergerak");
    }
}
 
public class Mobil extends Kendaraan {
    @Override
    public void bergerak() {
        System.out.println("Mobil melaju");
    }
}

Polymorphism: Many Forms

Polymorphism means one reference can point to objects of different types. The method called is determined at runtime:

Polymorphism at runtime
Kendaraan k = new Mobil();
k.bergerak();  // memanggil versi Mobil

Abstract Classes and Interfaces

Abstract Class as a Framework

An abstract class cannot be instantiated; it provides a framework that subclasses must complete. Abstract methods have no implementation and must be overridden:

Abstract class
public abstract class Bentuk {
    public abstract double luas();
}
 
public class Persegi extends Bentuk {
    private double sisi;
 
    public Persegi(double sisi) {
        this.sisi = sisi;
    }
 
    @Override
    public double luas() {
        return sisi * sisi;
    }
}

Modern Interfaces and Default Methods

An interface defines a contract that must be implemented. Since Java 8, interfaces can have default methods (with implementations) and static methods:

Interface with default methods
public interface Pembayaran {
    void bayar(double jumlah);
 
    default void cetakStruk() {
        System.out.println("Struk dicetak");
    }
 
    static void verifikasiSistem() {
        System.out.println("Sistem OK");
    }
}

A class implementing this interface must provide bayar, but may use the built-in cetakStruk.

Functional Interfaces

An interface with exactly one abstract method is called a functional interface — it becomes the basis of lambda expressions:

Functional interface with lambda
@FunctionalInterface
public interface Predikat {
    boolean uji(int nilai);
}
 
Predikat lebihDariSepuluh = n -> n > 10;
System.out.println(lebihDariSepuluh.uji(25));

The lambda n -> n > 10 concisely replaces an anonymous implementation. This pattern is widely used in the Stream API and modern frameworks.

Closing

Episode 6 builds the OOP foundation of Java: understanding classes and objects, applying encapsulation with accessors and mutators, building hierarchies with inheritance and polymorphism, using abstract classes as frameworks, and designing contracts with modern interfaces and functional interfaces.

Key takeaways:

  • A class is the blueprint; objects are created with new.
  • Encapsulation protects data through getters and setters.
  • Inheritance uses extends; polymorphism directs calls at runtime.
  • Abstract classes provide a framework that must be implemented.
  • Modern interfaces support default and static methods.
  • Functional interfaces enable lambdas for a single abstract method.

In the next episode, episode 7, we will discuss exception handling and basic debugging — the structure of checked and unchecked exceptions, try-catch-finally blocks, try-with-resources, rethrowing exceptions, creating custom exceptions, and debugging techniques in the IDE plus reading stack traces. Time to write robust code!

Learning Java - OOP Basics & Class Design | Learn Java