JAVA-00-Types of Classes in Java
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
- 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();
- 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();
- 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();
}
}
- 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:
Class is
final.All fields are
private&final.No setter methods.
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 Type | Characteristics | Example / Use Case |
| Concrete | Fully implemented | Car |
| Abstract | Partial implementation | Vehicle |
| Final | Cannot extend | MathUtils |
| Top-level | Defined in package | Student |
| Nested / Inner | Defined inside class | Outer.Inner, Anonymous |
| Singleton | Only one instance | Logger, Config |
| Immutable | State cannot change | String, Person |
| POJO | Simple object | Student |
| Java Bean | Serializable POJO with default constructor | Employee |
| Utility | Static helper methods | Utils, Math |