Skip to main content

Command Palette

Search for a command to run...

JAVA-00-Types of Classes in Java

Updated
•4 min read•View as Markdown

Types of Classes in Java – Complete Guid


1. Concrete Class

  • Definition: Fully implemented class. Can create objects.

  • Characteristics:

    • Can have instance variables and methods.

    • Can be inherited (unless final).

  • Example:

class Car {
    String brand;
    void drive() {
        System.out.println(brand + " is driving");
    }
}
  • Use Case: Any regular class you define for normal objects.

2. Abstract Class

  • Definition: Class with partial implementation. Can have abstract methods (without body).

  • Characteristics:

    • Cannot instantiate directly.

    • Can have constructors, methods, and fields.

    • Can be inherited by subclass which implements abstract methods.

  • Example:

abstract class Vehicle {
    abstract void start();
    void stop() {
        System.out.println("Vehicle stopped");
    }
}

class Bike extends Vehicle {
    void start() {
        System.out.println("Bike started");
    }
}
  • Use Case: Base classes in OOP, framework design.

3. Final Class

  • Definition: Class that cannot be extended.

  • Characteristics:

    • All methods are implicitly final.

    • Can still create objects.

  • Example:

final class MathUtils {
    static int square(int n) {
        return n * n;
    }
}
  • Use Case: Utility classes like Math, constants holder.

4. Top-level Class

  • Definition: Class defined directly inside a package, not inside another class.

  • Example:

package com.example;
public class Student {}
  • Use Case: Most classes in applications.

5. Nested / Inner Classes

  • Definition: Class defined inside another class.

Types of Nested / Inner Classes

  1. Non-static Inner Class – Requires outer object to instantiate.
class Outer {
    class Inner {
        void msg() { System.out.println("Hello from Inner"); }
    }
}
Outer o = new Outer();
Outer.Inner i = o.new Inner();
i.msg();
  1. Static Nested Class – Can be instantiated without outer object.
class Outer {
    static class Nested {
        void msg() { System.out.println("Hello from Nested"); }
    }
}
Outer.Nested n = new Outer.Nested();
n.msg();
  1. Local Class – Defined inside a method.
class Outer {
    void show() {
        class Local {
            void print() { System.out.println("Local class"); }
        }
        Local l = new Local();
        l.print();
    }
}
  1. Anonymous Class – No name, used for quick implementation.
Runnable r = new Runnable() {
    public void run() {
        System.out.println("Anonymous class running");
    }
};
new Thread(r).start();

6. Singleton Class

  • Definition: Only one instance exists.

  • Characteristics:

    • Private constructor.

    • Static method returns instance.

  • Example:

class Singleton {
    private static Singleton instance;
    private Singleton() {}
    public static Singleton getInstance() {
        if(instance == null) instance = new Singleton();
        return instance;
    }
}
  • Use Case: Logging, config manager, thread pool.

7. Immutable Class

  • Definition: Objects whose state cannot be changed after creation.

  • Rules:

    1. Class is final.

    2. All fields are private & final.

    3. No setter methods.

    4. Initialize fields via constructor.

  • Example:

final class Person {
    private final String name;
    Person(String name) { this.name = name; }
    public String getName() { return name; }
}
  • Use Case: Thread-safe classes, value objects.

8. POJO & Java Bean

  • POJO (Plain Old Java Object) – Simple class with private fields & public getters/setters.
class Student {
    private String name;
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}
  • Java Bean – Serializable POJO with default constructor + getters/setters.
import java.io.Serializable;
public class Employee implements Serializable {
    private String name;
    public Employee() {} // default constructor
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
}

9. Utility Class

  • Definition: Class with static methods only.

  • Example:

final class Utils {
    private Utils() {} // private constructor
    static int add(int a, int b) { return a + b; }
}
  • Use Case: Helper methods (Math, Collections).

✅ Summary Table – All Class Types

Class TypeCharacteristicsExample / Use Case
ConcreteFully implementedCar
AbstractPartial implementationVehicle
FinalCannot extendMathUtils
Top-levelDefined in packageStudent
Nested / InnerDefined inside classOuter.Inner, Anonymous
SingletonOnly one instanceLogger, Config
ImmutableState cannot changeString, Person
POJOSimple objectStudent
Java BeanSerializable POJO with default constructorEmployee
UtilityStatic helper methodsUtils, Math

More from this blog

SS-JAVA

23 posts