Skip to content

Add 60 comprehensive Java class examples to classes.md#5

Merged
janbodnar merged 2 commits intomainfrom
copilot/fix-ad3c0d6e-aa26-4b27-bfdc-94d1bfddab5c
Oct 7, 2025
Merged

Add 60 comprehensive Java class examples to classes.md#5
janbodnar merged 2 commits intomainfrom
copilot/fix-ad3c0d6e-aa26-4b27-bfdc-94d1bfddab5c

Conversation

Copy link
Contributor

Copilot AI commented Oct 7, 2025

This PR adds 60 progressively advanced Java examples to classes.md that comprehensively demonstrate object-oriented programming with classes using modern Java 25 syntax.

📋 What's Included

The examples cover all fundamental and advanced class-related topics:

Basic Concepts (Examples 1-12)

  • Class declaration and instantiation
  • Fields and methods
  • Constructors (default, parameterized, overloaded)
  • The this keyword and method chaining
  • Access modifiers (private, public, protected)
  • Static fields and methods

Inheritance & Polymorphism (Examples 13-21)

  • Basic inheritance with extends
  • Method overriding with @Override
  • The super keyword
  • Abstract classes and interfaces
  • Multiple interface implementation
  • Default and static methods in interfaces

Modern Java Features (Examples 22-30)

  • Records for immutable data carriers
  • Record validation and custom methods
  • Equals and hashCode implementation
  • ToString method overriding
  • Inner classes and static nested classes
  • Anonymous and local classes

Advanced Patterns (Examples 31-45)

  • Pattern matching with instanceof
  • Sealed classes for restricted hierarchies
  • Immutable class design
  • Builder and Singleton patterns
  • Generic classes with bounded type parameters
  • Composition over inheritance
  • Interface segregation
  • Dependency injection
  • Factory, Observer, Strategy, and Command patterns

Expert Topics (Examples 46-60)

  • Fluent interfaces
  • Enums with methods and fields
  • Functional interfaces with lambdas
  • Method references
  • Nested records
  • Varargs methods
  • Exception handling in classes
  • Polymorphism with interfaces
  • Static and instance initialization blocks
  • Copy constructors
  • Comparable interface
  • Custom annotations

🚀 Modern Java 25 Features

All examples use cutting-edge Java 25 syntax:

  • Instance main methods (void main() instead of public static void main)
  • Type inference with var keyword
  • Implicit imports (IO, List, Map, Set, ArrayList, HashMap)
  • Pattern matching with instanceof and sealed classes
  • Enhanced switch expressions with arrow syntax
  • Records for concise data classes

📝 Formatting & Style

  • Each example includes a brief description (max 80 characters per line, two-space line endings)
  • Progressive complexity from simple to advanced concepts
  • Consistent with existing documentation style (strings.md, loops.md, etc.)
  • Plain section titles without numbering

Example

Here's one of the 60 examples showing modern Java 25 syntax:

record Person(String name, int age) {}

void main() {
    var person = new Person("Alice", 30);
    IO.println(person.name() + " is " + person.age() + " years old");
}

Demonstrates Java records for immutable data carriers.


This comprehensive guide provides developers with a complete reference for mastering object-oriented programming in Java, from basic class concepts to advanced design patterns.

Original prompt

On classes.md, Create 60 concise and progressively advanced Java examples that showcase the usage of classes in Java. Each example should highlight a distinct concept or pattern related to object-oriented programming.

✅ Requirements

  • Use modern Java syntax, including features up to Java 25:
    • Compact source files (where appropriate)
    • Instance main methods
    • Type inference with var
    • Implicit imports (List, Map, IO, etc.)
  • Cover a variety of class-related topics:
    • Class declaration and instantiation
    • Fields and methods
    • Constructors (default and parameterized)
    • this keyword
    • Access modifiers (public, private, protected)
    • Static fields and methods
    • Inheritance and method overriding
    • Abstract classes and interfaces
    • Records (Java 16+)
    • Inner classes and anonymous classes
    • Object comparison (equals, hashCode)
    • Encapsulation and immutability
    • Pattern matching with instanceof (Java 21+)

📄 Format

class Person {

    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    void greet() {
        IO.println("Hello, I'm " + name + " and I'm " + age + " years old.");
    }
}

void main() {

    var p = new Person("Lena", 30);
    p.greet();
}

Description:
Defines a simple class with fields, constructor, and a method to greet.

🧠 Style Guidelines

  • Use compact source file format for examples that don’t require full class structure
  • Use traditional class syntax when demonstrating object-oriented features
  • Prefer IO.println for output
  • Use var for type inference
  • Keep examples readable and runnable
  • Include a brief description (max 80 characters per line, two-space line endings)
  • Refer to functions without parentheses (e.g. the main function)

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: janbodnar <16985640+janbodnar@users.noreply.github.com>
Copilot AI changed the title Create **60 concise and progressively advanced Java examples** that showcase the usage of **classes** in Java. Each example should highlight a distinct concept or pattern related to object-oriented programming. ### ✅ Requirements - Use **modern Java ... Add 60 comprehensive Java class examples to classes.md Oct 7, 2025
Copilot AI requested a review from janbodnar October 7, 2025 09:28
@janbodnar janbodnar marked this pull request as ready for review October 7, 2025 09:49
@janbodnar janbodnar merged commit 3a54e42 into main Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants