Classes and Types of Classes

Java 9 min min read Updated: Mar 31, 2026 Beginner
Classes and Types of Classes
Beginner Topic 19 of 25

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.

Key Concept: A class is a blueprint that defines variables and methods. Objects are created from classes, and Java provides multiple types of classes for different design purposes.

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

java class Student { String name; int age; void display() { System.out.println(name + " - " + age); } }

In this class:

  • name and age are variables
  • display() 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.

java public class Main { public static void main(String[] args) { Student s = new Student(); s.name = "Amit"; s.age = 20; s.display(); } }

Output

text Amit - 20

Here:

  • Student is the class
  • s is the object
  • new 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

java class ClassName { // variables // constructors // methods }

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

java class Car { void start() { System.out.println("Car started"); } }

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

java abstract class Animal { abstract void sound(); void sleep() { System.out.println("Animal sleeps"); } }

A subclass must provide implementation for the abstract method.

java class Dog extends Animal { void sound() { System.out.println("Dog barks"); } }

3. Final Class

A final class is a class declared with the final keyword. It cannot be extended or inherited.

Example

java final class Vehicle { }

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

java class Outer { int x = 10; class Inner { void show() { System.out.println("x = " + x); } } } public class Main { public static void main(String[] args) { Outer o = new Outer(); Outer.Inner i = o.new Inner(); i.show(); } }

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

java class Outer { static class Inner { void show() { System.out.println("Static nested class"); } } } public class Main { public static void main(String[] args) { Outer.Inner obj = new Outer.Inner(); obj.show(); } }

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

java class Outer { void display() { class LocalInner { void show() { System.out.println("Inside local inner class"); } } LocalInner obj = new LocalInner(); obj.show(); } }

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

java interface Greeting { void sayHello(); } public class Main { public static void main(String[] args) { Greeting g = new Greeting() { public void sayHello() { System.out.println("Hello from anonymous inner class"); } }; g.sayHello(); } }

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

java class Singleton { private static Singleton obj = new Singleton(); private Singleton() { } static Singleton getInstance() { return obj; } }

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:

  • private
  • protected

Valid Example

java public class Demo { }

Invalid Example

java private class Demo { } // Invalid

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 class
  • Person → abstract class
  • Constants → final or utility class
  • Student.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:

java class Demo { static { System.out.println("Static block"); } Demo() { System.out.println("Constructor"); } void show() { System.out.println("Method"); } } public class Main { public static void main(String[] args) { Demo d = new Demo(); d.show(); } }

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 new keyword
  • 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.

Quick Summary: A class is a blueprint for objects, and Java provides multiple types of classes such as concrete, abstract, final, inner, nested, local, and anonymous classes for different use cases.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators