In this article, we will look into various types of Classes in Java. So, let’s get started.

Different Types of Classes in Java
We have the following different types of Classes in Java
- Final Class
- Abstract Class
- Concrete Class
- Static Nested Class
- Inner Class
- Local Inner Class
- Anonymous Class
- Singleton Class
- Immutable Class
- POJO Class
Final Class
The Class marked with final keyword can not be inherited by a sub class. In simple words, the final class can not be extended.
public final class FinalClass {}
You can not do the below.
class childChild extends FinalClass{}
You will get compilation error.


Final classes are not intended to be extended, such as utility classes or classes that have a specific implementation that should not be modified.
Read More : final keyword in Java: Valuable Insights
Abstract Class
An abstract class in Java is a class that cannot be instantiated on its own and is designed to serve as a blueprint or template for other classes.
It may contain both abstract (unimplemented) methods and concrete (implemented) methods.
Abstract classes provide a way to define common behavior and structure that can be shared among multiple subclasses, while allowing each subclass to provide its own specific implementations.
Here’s a simple example Abstract class in Java.
Output
Read More : Abstract Class in Java: Comprehensive Guide
Concrete Class
A concrete class in Java is a class that can be instantiated, meaning you can create objects of this class. It provides concrete implementations of methods defined in its interfaces or abstract superclasses. Here’s an explanation along with a code example:
n this example, we have an abstract class Shape
with an abstract method draw()
. We then have two concrete classes Circle
and Rectangle
that extend the Shape
class and provide implementations for the draw()
method. These concrete classes can be instantiated, and objects of Circle
and Rectangle
can be created.
Static Nested Class
We can create a class within a class. Nested classes that are declared static are called static nested classes.
- static nested classes do not have access to any instance members of the enclosing outer class; it can only access them through an object’s reference
- static nested classes can access all static members of the enclosing class, including private ones
- Java programming specification doesn’t allow us to declare the top-level class as static; only classes within the classes (nested classes) can be made as static
- We don’t need to create an instance of Outer class for creating an instance of the static class.
Code Example
In this example, we have an OuterClass
that contains a static nested class named NestedClass
. The NestedClass
is accessed using the syntax OuterClass.NestedClass
, and you can create an instance of it just like you would with any other class.
Read More : Static Nested and Inner Classes in Java
Non-static Nested Class (Inner Class)
Nested classes that are non-static are called inner classes.
- It has access to all members of the outer class, including both static and instance members.
- It is accessed using an instance of the outer class:
outerInstance.new InnerClass()
. - It can be instantiated only within an instance of the outer class.
- Useful for creating logically related classes that need access to each other’s members.
Here’s an example of a non-static nested class (inner class) in Java:
In this example, the OuterClass
contains an inner class called InnerClass
. The inner class can access both instance variables and methods of the outer class, including private members, as demonstrated by the display()
method accessing the outerData
variable. To instantiate the inner class, you need to create an instance of the outer class and then use that instance to create an instance of the inner class.
Local Inner Class
Local inner class in Java is a class that is defined within a method or a block of code (such as a loop or conditional statement) inside another class or method.
- Local classes can only be accessed from inside the method or scope block in which they are defined.
- Local classes can access members (fields and methods) of its enclosing class just like regular inner classes.
- Local classes can also access local variables inside the same method or scope block, provided these variables are declared final.
- Local classes can also be declared inside static methods. In that case the local class only has access to the static parts of the enclosing class.
Example
Output
Read More : Local Inner Class in Java: Complete Guide
Anonymous Class
An anonymous class is a nested class that’s defined on the fly without a name.
We can instantiate an anonymous class from
- A Class (may be abstract or concrete).
- An Interface
Java Code Example
Output
Read More : Anonymous Class in Java: Comprehensive Guide
Singleton Class
Singleton class in Java ensures a class has only one instance and provides a global point of access to that instance.
Code Example
In this example, the Singleton
class follows the Singleton design pattern, ensuring that only one instance of the class can be created. The private constructor prevents external instantiation, and the getInstance()
method provides access to the single instance. The first time getInstance()
is called, it creates an instance of the class, and subsequent calls return the same instance. This ensures that there’s only one instance of the Singleton
class throughout the application.
Read More : Singleton Class in Java: Deep Insights
Immutable Class
An immutable class in Java is one whose instances cannot be changed after they have been created. This means that once an immutable class object is formed, its internal state cannot be altered.
For creating an immutable class in Java, we need to do the following things:
1. Make the class final
2. Make all the fields private and final.
3.Don’t provide any method that allows to change the values of the fields. For example don’t have any setter methods.Only getters should be there.
Java Code Example
Read More : Immutable Class in Java
POJO Class
A POJO (Plain Old Java Object) class is a Java class that encapsulates data and provides getter and setter methods to access and modify that data. POJOs are used to represent entities, data structures, or data transfer objects in a Java application. They are typically used in various layers of an application, such as the data access layer or the user interface layer.
Key characteristics of a POJO class include:
- Plainness: A POJO is a standard Java class without any special annotations or dependencies on specific frameworks.
- Encapsulation: It encapsulates data by using private instance variables and provides getter and setter methods to access and modify that data.
- Serializable: POJOs often implement the
Serializable
interface to enable serialization and deserialization. - No Business Logic: POJOs focus on representing data and do not contain complex business logic. They are primarily used for holding and transferring data between different parts of an application.
Java Code Example
Conclusion : Types of Classes in Java
In this article, we delved into different types of Classes in Java. We covered a spectrum of class types, from the unmodifiable nature of Final Classes to the flexible structure of POJO Classes. We delved into the concepts of Abstract Classes, Concrete Classes, Static Nested Classes, Inner Classes, Local Inner Classes, Anonymous Classes, Singleton Classes, and Immutable Classes, each bringing its own unique purpose and utility.