Classes and Types of Classes
In Java, a class is the most important building block of object-oriented programming. A class acts as a blueprint for creating objects. It defines the data that objects will store and the behavior they will perform.
Every Java program is built around classes. Even a very small Java program starts with a class declaration. Once you understand classes properly, it becomes much easier to understand objects, constructors, inheritance, and polymorphism.
What is a Class?
A class is a user-defined template that groups data and behavior into a single unit. In Java, data is represented using variables, and behavior is represented using methods.
A class may contain:
- instance variables
- static variables
- constructors
- methods
- blocks
- nested classes
Basic Class Example
In this class:
nameandageare variablesdisplay()is a method
Creating an Object from a Class
A class by itself is only a blueprint. To use it in practice, an object must be created.
Output
Here:
Studentis the classsis the objectnew Student()creates the object
Why Classes are Important
- They support object-oriented programming
- They group related data and methods together
- They make code modular and reusable
- They help in real-world modeling
- They improve maintainability and readability
General Syntax of a Class
Components of a Class
1. Variables
Variables store data related to the class or object.
2. Methods
Methods define behavior and operations.
3. Constructors
Constructors initialize the object when it is created.
4. Blocks
Static and instance blocks can be used for initialization logic.
Types of Classes in Java
Java provides multiple types of classes depending on structure and purpose. Commonly discussed types are:
- Concrete class
- Abstract class
- Final class
- Nested class
- Inner class
- Static nested class
- Local inner class
- Anonymous inner class
- Singleton class (design pattern based)
1. Concrete Class
A concrete class is a normal class that can be instantiated directly. It provides complete implementation of its methods.
Example
This class is complete and objects can be created from it.
2. Abstract Class
An abstract class is a class declared with the abstract keyword. It cannot be instantiated directly and may contain abstract methods as well as concrete methods.
Example
A subclass must provide implementation for the abstract method.
3. Final Class
A final class is a class declared with the final keyword. It cannot be extended or inherited.
Example
Attempting to inherit from this class will cause a compile-time error.
A well-known example from Java is the String class, which is final.
4. Nested Class
A nested class is a class declared inside another class. It helps in logically grouping classes that are used only in one place.
Nested classes are of two main types:
- Static nested class
- Inner class (non-static nested class)
5. Inner Class
An inner class is a non-static class declared inside another class. It can access all members of the outer class, including private members.
Example
6. Static Nested Class
A static nested class is declared with the static keyword inside another class. It behaves like a static member of the outer class.
Example
Unlike non-static inner classes, static nested classes do not require an object of the outer class.
7. Local Inner Class
A local inner class is declared inside a method or block. Its scope is limited to that method or block.
Example
This class cannot be accessed outside the method.
8. Anonymous Inner Class
An anonymous inner class is a class without a name. It is used when a class is needed only once, usually for interfaces or abstract classes.
Example with Interface
This is useful for quick implementations.
9. Singleton Class
A singleton class is a design pattern-based class that allows only one object to be created throughout the application.
Example
Singleton is useful for shared resources like configuration or database manager objects.
Access Modifiers and Classes
For top-level classes, only two access levels are allowed:
public- default (no modifier)
Top-level classes cannot be:
privateprotected
Valid Example
Invalid Example
Class Declaration vs Object Creation
| Point | Class | Object |
|---|---|---|
| Meaning | Blueprint | Instance of class |
| Memory | No separate object memory | Consumes memory when created |
| Example | Student |
new Student() |
Real-World Example of Different Classes
Suppose you are building a school management application:
Student→ concrete classPerson→ abstract classConstants→ final or utility classStudent.Address→ inner class
This shows how different class types help structure large systems properly.
Execution of a Class
A class itself is not executed directly. What executes are:
- static blocks during class loading
- constructors during object creation
- methods when invoked
Example:
Common Mistakes
- Confusing class with object
- Trying to create object of abstract class
- Trying to inherit a final class
- Using inner classes without understanding scope and access
- Writing overly large classes with too many responsibilities
Best Practices
- Use meaningful class names with PascalCase
- Keep classes focused on one responsibility
- Use abstract classes when partial implementation is needed
- Use final classes for immutable or restricted designs
- Use nested and inner classes only when they logically belong together
Interview-Oriented Points
- A class is a blueprint for objects
- Objects are created from classes using the
newkeyword - Concrete class can be instantiated directly
- Abstract class cannot be instantiated
- Final class cannot be inherited
- Inner class requires outer class object
- Static nested class does not require outer object
- Anonymous class has no explicit name
Conclusion
Classes are the foundation of Java object-oriented programming. They define the structure and behavior of objects and help organize code into reusable units.
Java supports different types of classes to solve different design problems. Understanding these class types is important for mastering object-oriented design, writing scalable applications, and answering interview questions confidently.

