Skip to main content

Command Palette

Search for a command to run...

Types of Classes in Java

Updated
โ€ข4 min read
Types of Classes in Java
N

I am a Tech Enthusiast having 13+ years of experience in ๐ˆ๐“ as a ๐‚๐จ๐ง๐ฌ๐ฎ๐ฅ๐ญ๐š๐ง๐ญ, ๐‚๐จ๐ซ๐ฉ๐จ๐ซ๐š๐ญ๐ž ๐“๐ซ๐š๐ข๐ง๐ž๐ซ, ๐Œ๐ž๐ง๐ญ๐จ๐ซ, with 12+ years in training and mentoring in ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐ƒ๐š๐ญ๐š ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐“๐ž๐ฌ๐ญ ๐€๐ฎ๐ญ๐จ๐ฆ๐š๐ญ๐ข๐จ๐ง ๐š๐ง๐ ๐ƒ๐š๐ญ๐š ๐’๐œ๐ข๐ž๐ง๐œ๐ž. I have ๐’•๐’“๐’‚๐’Š๐’๐’†๐’… ๐’Ž๐’๐’“๐’† ๐’•๐’‰๐’‚๐’ 10,000+ ๐‘ฐ๐‘ป ๐‘ท๐’“๐’๐’‡๐’†๐’”๐’”๐’Š๐’๐’๐’‚๐’๐’” and ๐’„๐’๐’๐’…๐’–๐’„๐’•๐’†๐’… ๐’Ž๐’๐’“๐’† ๐’•๐’‰๐’‚๐’ 500+ ๐’•๐’“๐’‚๐’Š๐’๐’Š๐’๐’ˆ ๐’”๐’†๐’”๐’”๐’Š๐’๐’๐’” in the areas of ๐’๐จ๐Ÿ๐ญ๐ฐ๐š๐ซ๐ž ๐ƒ๐ž๐ฏ๐ž๐ฅ๐จ๐ฉ๐ฆ๐ž๐ง๐ญ, ๐ƒ๐š๐ญ๐š ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ๐ข๐ง๐ , ๐‚๐ฅ๐จ๐ฎ๐, ๐ƒ๐š๐ญ๐š ๐€๐ง๐š๐ฅ๐ฒ๐ฌ๐ข๐ฌ, ๐ƒ๐š๐ญ๐š ๐•๐ข๐ฌ๐ฎ๐š๐ฅ๐ข๐ณ๐š๐ญ๐ข๐จ๐ง๐ฌ, ๐€๐ซ๐ญ๐ข๐Ÿ๐ข๐œ๐ข๐š๐ฅ ๐ˆ๐ง๐ญ๐ž๐ฅ๐ฅ๐ข๐ ๐ž๐ง๐œ๐ž ๐š๐ง๐ ๐Œ๐š๐œ๐ก๐ข๐ง๐ž ๐‹๐ž๐š๐ซ๐ง๐ข๐ง๐ . I am interested in ๐ฐ๐ซ๐ข๐ญ๐ข๐ง๐  ๐›๐ฅ๐จ๐ ๐ฌ, ๐ฌ๐ก๐š๐ซ๐ข๐ง๐  ๐ญ๐ž๐œ๐ก๐ง๐ข๐œ๐š๐ฅ ๐ค๐ง๐จ๐ฐ๐ฅ๐ž๐๐ ๐ž, ๐ฌ๐จ๐ฅ๐ฏ๐ข๐ง๐  ๐ญ๐ž๐œ๐ก๐ง๐ข๐œ๐š๐ฅ ๐ข๐ฌ๐ฌ๐ฎ๐ž๐ฌ, ๐ซ๐ž๐š๐๐ข๐ง๐  ๐š๐ง๐ ๐ฅ๐ž๐š๐ซ๐ง๐ข๐ง๐  new subjects.

In Java, a class is a blueprint for creating objects. It defines the attributes (variables) and behaviors (methods) of an object. Java supports multiple types of classes based on their usage and scope.

Normal (Concrete) Class

A normal class is a standard Java class that contains variables, methods, and constructors. It can be instantiated directly to create objects.

Example:

// Normal class example
class Car {
    String brand;
    int speed;

    // Constructor
    Car(String brand, int speed) {
        this.brand = brand;
        this.speed = speed;
    }

    // Method
    void display() {
        System.out.println("Brand: " + brand + ", Speed: " + speed + " km/h");
    }

    public static void main(String[] args) {
        Car myCar = new Car("Toyota", 180);
        myCar.display();
    }
}

Use Case: Used in most Java applications for object-oriented programming.

Abstract Class

An abstract class is a class that cannot be instantiated. It can contain both abstract methods (without implementation) and concrete methods (with implementation).

Example:

// Abstract class
abstract class Animal {
    abstract void makeSound(); // Abstract method

    void sleep() {
        System.out.println("Sleeping...");
    }
}

// Concrete subclass
class Dog extends Animal {
    void makeSound() {
        System.out.println("Barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        myDog.makeSound();
        myDog.sleep();
    }
}

Use Case: Used for defining a base class with common functionality but requiring subclasses to implement specific behaviors.

Final Class

A final class cannot be inherited by other classes. It is used to prevent modification of class behavior.

Example:

// Final class
final class Calculator {
    int add(int a, int b) {
        return a + b;
    }
}

// The following will cause an error if uncommented
// class AdvancedCalculator extends Calculator {} // ERROR: Cannot extend final class

public class Main {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println("Sum: " + calc.add(5, 10));
    }
}

Use Case: Used when a class should not be extended, such as String class in Java.

Static Class (Nested Static Class)

Java does not support standalone static classes, but a class can be made static inside another class.

Example:

class Outer {
    static class NestedStaticClass {
        void display() {
            System.out.println("Static Nested Class");
        }
    }

    public static void main(String[] args) {
        Outer.NestedStaticClass obj = new Outer.NestedStaticClass();
        obj.display();
    }
}

Use Case: Used for grouping related utility methods inside another class.

Inner Class (Non-Static Nested Class)

An inner class is a class defined within another class. It has access to the outer class's variables and methods.

Example:

class OuterClass {
    int x = 10;

    class InnerClass {
        void display() {
            System.out.println("Value of x: " + x);
        }
    }

    public static void main(String[] args) {
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();
        inner.display();
    }
}

Use Case: Used when a class logically belongs to another class.

Anonymous Class

An anonymous class is a class without a name, usually used for implementing an interface or extending a class inline.

Example:

abstract class Vehicle {
    abstract void start();
}

public class Main {
    public static void main(String[] args) {
        // Anonymous class implementing Vehicle
        Vehicle car = new Vehicle() {
            void start() {
                System.out.println("Car is starting...");
            }
        };
        car.start();
    }
}

Use Case: Used for quick, one-time use implementations.

Singleton Class

A singleton class ensures that only one instance of the class exists throughout the application.

Example:

class Singleton {
    private static Singleton instance;

    private Singleton() {} // Private constructor

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }

    void showMessage() {
        System.out.println("Singleton Instance");
    }
}

public class Main {
    public static void main(String[] args) {
        Singleton obj1 = Singleton.getInstance();
        obj1.showMessage();
    }
}

Use Case: Used in database connections, caching, and logging.

POJO (Plain Old Java Object) Class

A POJO class is a simple Java class with private fields and getter/setter methods.

Example:

class Employee {
    private String name;
    private int salary;

    // Constructor
    public Employee(String name, int salary) {
        this.name = name;
        this.salary = salary;
    }

    // Getters
    public String getName() {
        return name;
    }

    public int getSalary() {
        return salary;
    }
}

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee("Alice", 50000);
        System.out.println("Employee Name: " + emp.getName());
    }
}

Use Case: Used in data transfer objects (DTOs) in frameworks like Hibernate and Spring.

Record Class (Java 14+)

A record class is a special Java class introduced in Java 14 that acts as a data carrier.

Example:

record Person(String name, int age) {}

public class Main {
    public static void main(String[] args) {
        Person p = new Person("John", 30);
        System.out.println(p.name() + " is " + p.age() + " years old.");
    }
}

Use Case: Used for immutable data objects with reduced boilerplate code.

More from this blog

Naveen P.N's Tech Blog

95 posts