Java OOP Cheat Sheet
Covers classes, constructors, inheritance, polymorphism, interfaces with default methods, and Java's access modifiers with examples.
2 PagesBeginnerApr 8, 2026
Classes & Objects
Define fields, a constructor, and methods, then instantiate an object.
java
public class Car { private String model; // encapsulated field private int speed; public Car(String model) { // constructor this.model = model; this.speed = 0; } public void accelerate(int amount) { this.speed += amount; } public String getModel() { return model; } // getter}Car myCar = new Car("Tesla"); // instantiationmyCar.accelerate(50);
Inheritance & Polymorphism
Extend a class and override behavior that's resolved at runtime.
java
public abstract class Shape { public abstract double area(); // must be implemented by subclasses public void describe() { System.out.println("Area: " + area()); }}public class Circle extends Shape { private double radius; public Circle(double radius) { this.radius = radius; } @Override public double area() { return Math.PI * radius * radius; }}Shape s = new Circle(3.0); // upcastings.describe(); // polymorphic call - runs Circle's area()
Interfaces
Define a contract, including default and static methods (Java 8+).
java
public interface Drivable { void drive(); // implicitly public abstract default void honk() { // default method (Java 8+) System.out.println("Beep!"); } static Drivable simple() { // static interface method return () -> System.out.println("Driving..."); }}public class Truck implements Drivable { @Override public void drive() { System.out.println("Truck driving"); }}
Four Pillars of OOP
The core concepts every Java class hierarchy is built on.
- Encapsulation- Hiding internal state behind private fields and exposing controlled access via public methods
- Inheritance- A subclass (extends) reuses and extends the fields/methods of a superclass
- Polymorphism- A superclass reference can invoke overridden subclass behavior at runtime (dynamic dispatch)
- Abstraction- abstract classes and interfaces define a contract without full implementation
- this- Refers to the current instance
- super- Calls the parent class's constructor or overridden method
- @Override- Annotation that verifies a method actually overrides a superclass/interface method
Access Modifiers
Control visibility of fields and methods across classes and packages.
java
public class Account { public String owner; // accessible from anywhere protected double balance; // accessible in package + subclasses String branch; // package-private (no modifier) private String pin; // accessible only within this class private boolean validatePin(String input) { return input.equals(pin); }}
Pro Tip
Favor composition ('has-a', embedding an object as a field) over deep inheritance chains ('is-a') - it keeps classes easier to test and avoids the fragile base class problem where changes to a superclass unexpectedly break distant subclasses.
Was this cheat sheet helpful?
Explore Topics
#JavaOOP#JavaOOPCheatSheet#Programming#Beginner#ClassesObjects#InheritancePolymorphism#Interfaces#FourPillarsOfOOP#OOP#Functions#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance