QUICK REFERENCE

Java OOP Cheat Sheet

The essential syntax, patterns, and boilerplate every Java LLD engineer needs at their fingertips.

Basics

Class Anatomy

The fundamental structure of a Java class.

public class Dog {
static String species = "Canis"; // Class Var
private String name; // Instance Var
public Dog(String name) { // Constructor
this.name = name;
}
public void bark() {
System.out.println(this.name + " says Woof!");
}
}
Core

Encapsulation

Using private fields and public getters/setters.

public class Account {
private double balance; // Data Hiding
public double getBalance() {
return balance;
}
public void setBalance(double amount) {
if (amount >= 0) { // Validation logic
this.balance = amount;
}
}
}
Inheritance

Inheritance

Extending classes using the 'extends' keyword.

class Animal {
void speak() { System.out.println("..."); }
}
class Dog extends Animal {
@Override
void speak() {
super.speak(); // Call parent method
System.out.println("Woof!");
}
}
Polymorphism

Overloading

Compile-time polymorphism (Same method, diff params).

class Calculator {
// Method Overloading
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
Abstraction

Interfaces

Contracts that define 'what' not 'how'. Supports multiple inheritance.

interface Flyable {
void fly(); // Abstract by default
}
class Bird implements Flyable {
@Override
public void fly() {
System.out.println("Flapping wings...");
}
}
Abstraction

Abstract Classes

Partial implementation. Can have both abstract and concrete methods.

abstract class Vehicle {
abstract void move(); // No body
void fuel() { // Concrete method
System.out.println("Refueling...");
}
}
Modern Java

Java Records

Immutable data carriers (Java 14+). Concise data classes.

// Automatically generates constructor,
// getters, equals(), hashCode(), toString()
public record User(int id, String username) {}
User u = new User(1, "admin");
System.out.println(u.username());
Utils

Collections

Essential data structures for LLD.

import java.util.*;
List<String> list = new ArrayList<>();
list.add("Item");
Map<String, Integer> map = new HashMap<>();
map.put("Key", 100);
Set<String> unique = new HashSet<>();
Patterns

Thread-Safe Singleton

Double-checked locking implementation.

public class Database {
private static volatile Database instance;
private Database() {} // Private constructor
public static Database getInstance() {
if (instance == null) {
synchronized (Database.class) {
if (instance == null) instance = new Database();
}
}
return instance;
}
}