Pending
Types of Classes in Java
In Java, classes can be categorized based on their usage, structure, and purpose.
Understanding the different types of classes helps in designing efficient and
modular object-oriented programs.
Concrete Class:
A concrete class is a regular class that can be instantiated. It contains complete
implementations of all its methods and serves as the most commonly used class type
in Java. You define fields, methods, and constructors in a concrete class.
Abstract Class:
An abstract class is a class declared with the abstract keyword and may contain
both fully implemented methods and abstract methods (without implementation). It
cannot be instantiated directly; instead, it is used as a base class to be extended
by other classes that provide implementations for its abstract methods.
Final Class:
A final class is declared using the final keyword and cannot be extended
(inherited) by any other class. This is used to prevent modification of class
behavior through inheritance. For example, the String class in Java is a final
class.
Static Nested Class:
A static nested class is a static class defined inside another class. It does not
require an instance of the outer class to be instantiated. It can access only the
static members of the outer class.
Non-static (Inner) Class:
An inner class is defined inside another class but is non-static. It can access
both static and non-static members of the outer class and requires an instance of
the outer class to be instantiated.
Local Inner Class:
A local inner class is defined inside a method or a block of code. It is local to
the method where it is defined and cannot be accessed from outside that method.
Anonymous Inner Class:
An anonymous inner class is a local class without a name. It is used to instantiate
a class and define its methods all at once, usually for implementing interfaces or
extending classes in a single-use context.
POJO (Plain Old Java Object):
A POJO is a simple class that doesn't follow any special Java conventions or
frameworks. It typically contains private fields, public getters/setters, and a no-
argument constructor. It is used for representing simple data models.
Utility Class:
A utility class contains only static methods and fields and is not meant to be
instantiated. These classes often provide common functionality, such as helper or
tool methods. For example, the Math and Collections classes in Java are utility
classes.
Wrapper Classes:
Wrapper classes are predefined classes provided by Java to wrap primitive data
types into objects. Examples include Integer, Double, Character, Boolean, etc.