Classes & Objects (OOP)
Java is a fully object-oriented language — all code must live inside a class. The main OOP concepts: class (blueprint), object (instance of a class), inheritance, encapsulation, polymorphism, and abstraction.
Encapsulation combines data (fields) and methods into a single class, controlling access through modifiers: public (everyone can access), protected (package + subclasses), default (package only), private (class only). Getters/setters provide controlled access to private fields.
Inheritance allows a child class to inherit fields and methods from its parent. Java supports single inheritance (one parent) but can implement multiple interfaces. Polymorphism allows objects to behave differently depending on the context — through method overriding (runtime) and method overloading (compile-time).
Java OOP Concepts:
| Concept | Keyword | Example |
|---|---|---|
| Class | class |
class Cat { ... } |
| Inheritance | extends |
class Anabul extends Animal |
| Interface | implements |
class Cat implements DapatBerpelihara |
| Abstraction | abstract |
abstract class Shape { ... } |
| Encapsulation | Access modifiers | private String name; |
Code Example
// Abstract class
abstract class Animal {
private String name;
Animal(String name) {
this.name = name;
}
String getName() { return name; }
abstract String sound();
void info() {
System.out.println(name + " bersound: " + sound());
}
}
// Interface
interface DapatBerpelihara {
default String infoPeliharaan() {
return "Can be kept at home";
}
}
// Inheritance + interface
class Cat extends Animal implements DapatBerpelihara {
private String color;
Cat(String name, String color) {
super(name);
this.color = color;
}
@Override
String sound() { return "Meong!"; }
@Override
public String toString() {
return "Cat{" + getName() + ", " + color + "}";
}
}
class Dog extends Animal {
Dog(String name) { super(name); }
@Override
String sound() { return "Guk guk!"; }
}
public class OOPDemo {
public static void main(String[] args) {
Animal kucing = new Cat("Kitty", "oranye");
Animal anjing = new Dog("Buddy");
kucing.info(); // Kitty bersound: Meong!
anjing.info(); // Buddy bersound: Guk guk!
// instanceof with pattern matching
if (kucing instanceof DapatBerpelihara d) {
System.out.println(d.infoPeliharaan());
}
}
}
Code Explanation
- Abstract class:
Animalcannot be instantiated directly — it must be extended. The abstract methodsound()must be implemented by every subclass. - Interface:
DapatBerpeliharadefines a contract. Adefaultmethod has a built-in implementation. A class can implement many interfaces. - Constructor:
Animal(String name)withthis.name = namedistinguishes the parameter from the field. - @Override: an annotation marking a method that overrides a parent method — the compiler will error if the signature does not match.
- Polymorphism:
Animal kucing = new Cat(...)— the reference is typed as the parent, the object is typed as the child. Thesound()method that gets called is the child's version (dynamic dispatch).
Everyday Example
A library management system using OOP:
abstract class Book {
private String title;
private String author;
private boolean available;
Book(String title, String author) {
this.title = title;
this.author = author;
this.available = true;
}
String getTitle() { return title; }
boolean isAvailable() { return available; }
void borrow() { available = false; }
void returnBook() { available = true; }
abstract double lateFee(int daysLate);
}
class PhysicalBook extends Book {
PhysicalBook(String title, String author) { super(title, author); }
@Override
double lateFee(int day) { return day * 5000; }
}
class DigitalBook extends Book {
DigitalBook(String title, String author) { super(title, author); }
@Override
double lateFee(int day) { return 0; }
}
public class Library {
public static void main(String[] args) {
Book[] catalog = {
new PhysicalBook("Bumi", "Tere Liye"),
new DigitalBook("Laskar Pelangi", "Andrea Hirata")
};
for (Book b : catalog) {
System.out.println(b.getTitle() + " lateFee/day: $" + b.lateFee(3));
}
}
}
Summary
OOP is Java's main paradigm, enabling structured, reusable, and easy-to-maintain code. Abstract classes and interfaces provide strong abstractions. Polymorphism enables flexible, extensible code. Master these concepts before moving on to design patterns and frameworks.