Object-Oriented Patterns: Classes, Interfaces, Inheritance

Key takeaway: Practice Java with 33+ clean, runnable examples covering arrays, strings, classes, interfaces, exceptions, collections, and streams — all with expected output to verify instantly.

Java is a robust, object-oriented programming language. These examples are designed for students, interview candidates, backend engineers, and developers learning Java hands-on.

Last updated: October 2026 · 33 examples

Quick Start

  1. Pick an example from the sidebar or mobile menu
  2. Study the Java source code and expected output
  3. Run it in our online Java compiler or your local JDK
  4. Modify the code to experiment with inputs and methods

Who is this for: CS students, job seekers preparing for Java interviews, self-taught coders, and developers transitioning to Java. Use this when: you need quick syntax reference, practice problem-solving, or build foundational fluency.

Related:Java Compiler · Python Examples · JavaScript Examples · TypeScript Examples

Java Program with Interface Implementation

Open in Compiler

Implements a simple interface.

Program (Java)
public class Main {
    interface Shape { double area(); }
    static class Circle implements Shape {
        double r; Circle(double r){ this.r = r; }
        public double area(){ return Math.PI * r * r; }
    }
    public static void main(String[] args) {
        Shape c = new Circle(3);
        System.out.println(String.format("%.2f", c.area()));
    }
}
Expected Output
28.27
Program Output (Execution)

Java Program: Class and Object Basics

Open in Compiler

Defines a simple class and creates an object.

Program (Java)
public class Main {
    static class Person {
        String name;
        Person(String name) { this.name = name; }
        String greet() { return "Hello, " + name; }
    }
    public static void main(String[] args) {
        Person p = new Person("Alice");
        System.out.println(p.greet());
    }
}
Expected Output
Hello, Alice
Program Output (Execution)

Java Program Demonstrating Inheritance

Open in Compiler

Shows a subclass extending a base class.

Program (Java)
public class Main {
    static class Animal { String sound(){ return "..."; } }
    static class Dog extends Animal { @Override String sound(){ return "Woof"; } }
    public static void main(String[] args) {
        Dog d = new Dog();
        System.out.println(d.sound());
    }
}
Expected Output
Woof
Program Output (Execution)

Java Program with Abstract Class

Open in Compiler

Demonstrates abstract class and method.

Program (Java)
public class Main {
    static abstract class Vehicle { abstract int wheels(); }
    static class Bike extends Vehicle { int wheels(){ return 2; } }
    public static void main(String[] args) {
        Vehicle v = new Bike();
        System.out.println(v.wheels());
    }
}
Expected Output
2
Program Output (Execution)

Keep Practicing in Online Java Compiler

Use our free online Java compiler to run all examples, test code variations, and debug errors in your browser.

Launch Java Compiler

Frequently Asked Questions

Install a JDK (Java Development Kit), then compile with javac Main.java and run with java Main. On macOS, Linux, or Windows, use the official OpenJDK builds or your package manager.

The examples target modern Java (Java 8 and above). If you use older versions, most examples will still work, but features like lambdas and streams require Java 8+.

Yes. These examples are intended for learning. Copy the code, experiment with changes (e.g., different inputs), and observe the output to deepen your understanding of Java fundamentals.

Yes. Java is platform-independent. The examples compile and run on major platforms (Windows, macOS, Linux) with a standard JDK.

Learn Java the Practical Way

Java powers enterprise applications, Android development, and countless backend systems. Practicing small, focused programs builds fluency with syntax and core concepts such as data types, control flow, methods, classes, interfaces, exceptions, collections, and streams.

On this page, you will find clean, well-formatted examples that run across platforms. Each example includes a short description and expected output so you can quickly verify your understanding. Explore the topics via the sidebar and extend the code to experiment further.

Whether you are preparing for interviews, working through coursework, or refreshing fundamentals, these examples offer a practical path to mastering Java. Bookmark the page and return often to keep improving.

Java Programming Examples — Learn by Doing

This collection of Java examples covers fundamental to intermediate concepts with clean, runnable code. Each program is self-contained — copy it into any Java IDE, online compiler, or your local development environment and run it immediately. The examples progress from basic syntax through object-oriented programming, data structures, and algorithms.

Java remains one of the most popular programming languages worldwide, ranking consistently in the top 3 on TIOBE, Stack Overflow, and GitHub. It powers Android apps, enterprise backends (Spring Boot), big data (Hadoop), and financial systems. Practising with small, focused examples is the fastest way to build Java fluency.

Topics Covered in These Examples

  • Basics: Hello World, variables, data types, operators
  • Control Flow: if/else, switch, for, while, do-while
  • Methods: Parameters, return values, overloading, recursion
  • Arrays: Declaration, iteration, sorting, searching
  • Strings: Manipulation, StringBuilder, comparisons
  • Math: Factorial, Fibonacci, prime numbers, GCD
  • OOP: Classes, objects, constructors, inheritance
  • Interfaces: Abstraction, polymorphism, default methods
  • Exceptions: try/catch, custom exceptions, throws
  • Collections: ArrayList, HashMap, HashSet, iterators
  • Streams: filter, map, reduce, collect (Java 8+)
  • File I/O: Reading, writing, BufferedReader, Files API

How to Use These Examples

  1. Browse by topic — use the sidebar to jump to a specific category (loops, arrays, OOP, etc.)
  2. Read the description — each example includes a brief explanation of what the program does
  3. Copy and run the code — paste into our Java Compiler or any local IDE
  4. Check the expected output — compare your result with the listed output
  5. Modify and experiment — change values, add features, or combine examples to deepen understanding

Why Practice with Small Programs?

Research on skill acquisition shows that deliberate practice — focused, repetitive work on specific concepts — produces faster learning than reading alone. Small Java programs isolate one concept at a time, making it easy to understand, modify, and remember. This is why coding interviews test with small problems: they reveal fundamental fluency that large project experience alone cannot demonstrate.

Each example on this page is designed to be completed in 2–5 minutes, making them perfect for daily practice sessions, interview preparation, or quick reference when you forget a specific syntax pattern.

Java Development Environment

  • Online (no install): Use our free Java Compiler to run any example instantly in your browser
  • Local IDE: IntelliJ IDEA (recommended), Eclipse, VS Code with Java Extension Pack
  • JDK: Install OpenJDK 17+ (LTS) for full compatibility with modern Java features
  • Build tools: Maven or Gradle for larger projects (not needed for these examples)

Frequently Asked Questions

What Java version do these examples use?

Most examples use Java 8+ features. Some advanced examples use Java 17 features like records and text blocks. All run on the current LTS (Java 17 or 21).

Can I use these for interview preparation?

Yes. The examples cover the most common interview topics: arrays, strings, recursion, OOP, collections, and basic algorithms. Practice modifying them to solve variations.

Why does every example use public class Main?

In Java, the class name must match the filename. Using Main makes examples portable across online compilers that expect this convention. In real projects, you would name classes descriptively.

Are these examples free to use?

Yes, completely free. Copy, modify, and use them in your projects, homework, or learning materials without attribution.

Who Uses This Page?

CS students practising for exams and assignments. Job seekers preparing for Java technical interviews. Self-taught developers building foundational Java skills. Teachers looking for clean, copyable code examples for lessons. Android developers refreshing core Java knowledge. Backend engineers transitioning from other languages to Java/Spring Boot.