For the complete documentation index, see llms.txt. This page is also available as Markdown.

Types of Classes

About

Java provides different types of classes to support various programming needs.

1. Concrete Class

A concrete class is a regular class that can be instantiated into objects. It contains both implemented methods and instance variables.

Characteristics

  • Can be instantiated (objects can be created from it).

  • Can have constructors, methods, and fields.

  • Can be extended by other classes (supports inheritance).

  • Can implement interfaces.

  • Cannot have abstract methods.

Example

2. Abstract Class

An abstract class cannot be instantiated but serves as a base class for other classes. It can have both abstract methods (without implementation) and concrete methods.

Characteristics

  • Cannot be instantiated directly.

  • Can contain both abstract and concrete methods.

  • Must be extended by a subclass.

  • Supports constructors and fields.

  • Can implement interfaces.

Example

3. Final Class

A final class cannot be extended (no subclass can inherit from it). It is used to prevent modification of critical functionality.

Characteristics

  • Cannot be extended (inherited).

  • Can contain fields, methods, and constructors.

  • Often used for security and immutability.

  • Can be instantiated normally.

Example

4. Static Class (Nested Static Class)

Java does not allow top-level static classes, but it allows static nested classes inside another class.

Characteristics

  • A static nested class can be created inside another class.

  • Cannot access non-static members of the outer class directly.

  • Used when the nested class is logically related to the outer class but does not depend on its instance.

Example

5. Inner Class (Non-Static Nested Class)

An inner class is a class defined inside another class and has access to its outer class's members.

Characteristics

  • Can access private members of the outer class.

  • Requires an instance of the outer class to be created.

  • Useful for encapsulation and logical grouping.

Example

6. Anonymous Class

An anonymous class is a class without a name, defined on the fly for one-time use, often used with interfaces or abstract classes.

Characteristics

  • Declared inside a method or as part of an expression.

  • Cannot have constructors.

  • Typically used to implement interfaces or extend abstract classes.

Example

7. POJO (Plain Old Java Object) Class

A POJO class is a simple Java class that only contains fields, getters, and setters. It does not extend or implement anything special.

Characteristics

  • Only contains fields (variables) and getter/setter methods.

  • Used as data holders.

  • No business logic.

Example

Comparison

Type of Class

Can be Instantiated?

Supports Inheritance?

Special Feature

Concrete Class

✅ Yes

✅ Yes

Regular class used for objects

Abstract Class

❌ No

✅ Yes

Can have abstract & concrete methods

Final Class

✅ Yes

❌ No

Cannot be extended

Static Class

✅ Yes (nested)

❌ No

Can be a static nested class

Inner Class

✅ Yes (inside another class)

❌ No

Can access outer class members

Anonymous Class

✅ Yes (one-time use)

❌ No

No name, used inside methods

POJO Class

✅ Yes

✅ Yes

Plain Java Object with getters/setters

Last updated