Practice OOP Basics in Java

Object-Oriented Programming fundamentals including classes, objects, and encapsulation.

Work through 24 exercises with starter code, expected output, test cases, and hints.

How to learn OOP Basics

Object-oriented design groups state with the operations that keep that state valid. A class is useful when it protects an invariant or gives a domain idea a clear name; creating getters, setters, and inheritance for every noun is not a design goal by itself.

Begin with one small class and one responsibility. Decide what must be true after construction, keep fields private when callers should not change them freely, and expose behavior that describes intent. This makes the object easier to test and harder to misuse.

Recommended learning order

  1. Classes and instances: Define fields and methods, create objects with new, and understand that each instance has its own state while static members belong to the class.
  2. Construct valid objects: Use constructors to require essential values. Reject invalid inputs at the boundary instead of allowing a half-valid object.
  3. Encapsulate behavior: Keep representation details private and add methods that perform valid state transitions, such as deposit rather than setBalance.
  4. Use polymorphism deliberately: After composition and simple classes are clear, study interfaces and overriding. Depend on the smallest useful contract.

Tested example: Protect a class invariant

The constructor prevents a negative opening balance, and deposit is the only public way to increase it. Callers can observe the balance but cannot assign an arbitrary value.

public class Main {
    static final class Account {
        private int balance;

        Account(int openingBalance) {
            if (openingBalance < 0) throw new IllegalArgumentException("negative balance");
            balance = openingBalance;
        }

        void deposit(int amount) {
            if (amount <= 0) throw new IllegalArgumentException("deposit must be positive");
            balance += amount;
        }

        int balance() { return balance; }
    }

    public static void main(String[] args) {
        Account account = new Account(100);
        account.deposit(25);
        System.out.println(account.balance());
    }
}

Expected output

125

Change and run this example in the Java compiler

Common mistakes

  • Making every field public, which lets callers bypass validation and break object invariants.
  • Adding setters mechanically even when a value should never change after construction.
  • Confusing an object reference with the object itself; two variables can refer to the same mutable instance.
  • Using static fields for per-object state, causing all instances to share a value accidentally.

How to practice

Build classes before class hierarchies. After each exercise, describe the invariant in one sentence and check whether any public method can break it. Then continue to the inheritance hub to study substitutable types and interfaces.

Exercises to do first

  • Create a class — Define state and behavior for one small domain object.
  • Constructor — Require essential values when the object is created.
  • Getters and setters — Expose only the access that the class contract genuinely needs.

OOP Basics exercises

  1. Create a Simple Class Java exercise — Define a class with attributes and methods
  2. Using Constructors Java exercise — Create a class with a constructor
  3. Getters and Setters Java exercise — Implement encapsulation with getters and setters
  4. Static Members Java exercise — Use static fields and methods
  5. Method Overloading Java exercise — Create overloaded methods
  6. Using 'this' Keyword Java exercise — Use 'this' to refer to current instance
  7. BMI Calculator Class Java exercise — Create a class to calculate BMI
  8. Student Grade Calculator Java exercise — Create a class to manage student grades
  9. Bank Account Operations Java exercise — Create a full bank account class
  10. Shopping Cart Java exercise — Create a simple shopping cart system
  11. Circle Class Java exercise — Create a Circle class with area and circumference
  12. Employee Salary Calculator Java exercise — Calculate employee net salary
  13. Time Class Java exercise — Create a Time class for time operations
  14. Fraction Class Java exercise — Create a Fraction class with arithmetic operations
  15. Temperature Converter Java exercise — Create a temperature conversion class
  16. Point Distance Calculator Java exercise — Calculate distance between two points
  17. Inventory Management Java exercise — Create an inventory system with products
  18. Rational Number Class Java exercise — Create a Rational class for fractions
  19. Music Playlist Manager Java exercise — Create a playlist management system
  20. Singleton Pattern Java exercise — Implement the singleton design pattern
  21. Factory Pattern Java exercise — Implement the factory design pattern
  22. Builder Pattern Java exercise — Implement the builder design pattern
  23. Observer Pattern Java exercise — Implement the observer design pattern
  24. Decorator Pattern Java exercise — Implement the decorator design pattern

Browse the complete Java practice path · Open the online Java compiler