# Types of Classes in Java

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:**

```java
// 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:**

```java
// 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:**

```python
// 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:**

```java
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:**

```java
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:**

```java
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:**

```java
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:**

```java
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:**

```python
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.
