Types of Classes in Java
Java provides different types of classes to serve specific purposes in your application. Understanding these class types helps you write better organized and efficient code.
1. Final Class
Final Keyword in Java:
- A
finalvariable cannot change its value once assigned - A
finalmethod cannot be overridden by a subclass - A
finalclass cannot be inherited
✔ Use Cases
Useful for creating immutable classes, like the String class in Java.
- If a class is final, it cannot be changed or extended
- If only its members are final, the class can still be extended, but final members cannot be changed
// Final class: cannot be inherited
final class Building {
// Final variable: cannot be changed
final int floors = 5;
// Final method: cannot be overridden
final void fireSafety() {
System.out.println("Follow fire safety rules.");
}
}
// This would cause an error because Building is final
// class House extends Building {}
public class Main {
public static void main(String[] args) {
Building b = new Building();
System.out.println("Number of floors: " + b.floors);
b.fireSafety();
}
}Output:
Number of floors: 5Follow fire safety rules.2. Static Class
A static class is a nested class declared inside another class with the static keyword.
Key Characteristics:
- Does not need an object of the outer class to be created
- Can access only static members of the outer class
- Useful for grouping classes that are only related to the outer class
class Building {
static String type = "Skyscraper";
// Static nested class
static class Room {
void showRoom() {
System.out.println("This is a room in a " + type);
}
}
}
public class Main {
public static void main(String[] args) {
// No need to create Building object
Building.Room r = new Building.Room();
r.showRoom();
}
}Output:
This is a room in a Skyscraper3. Abstract Class
A class with one or more abstract methods and declared with the abstract keyword.
Important Points:
- It is incomplete, so we must extend it in a concrete subclass to use it
- Can have constructors, static methods, and final methods
- Final methods cannot be changed by subclasses
- You cannot create an object of an abstract class directly because it may have abstract methods without a body
abstract class Building {
abstract void openDoor();
}
// Concrete subclass
class House extends Building {
void openDoor() {
System.out.println("House door opened.");
}
}
public class Main {
public static void main(String[] args) {
// Building b = new Building(); // Not allowed
House h = new House(); // Allowed
h.openDoor();
}
}Output:
House door opened.4. Concrete Class
A class that has complete implementations for all its methods and no abstract methods.
Characteristics:
- It is a fully working class that can be instantiated (objects can be created)
- Can extend an abstract class or implement an interface
- Must provide implementations for all abstract methods from parent class
// Abstract parent class
abstract class Building {
abstract void openDoor(); // abstract method
}
// Concrete class that extends abstract class
class ConcreteBuilding extends Building {
@Override
void openDoor() {
System.out.println("Door opened in the building.");
}
void turnOnLights() {
System.out.println("Lights are turned on.");
}
}
public class Main {
public static void main(String[] args) {
ConcreteBuilding building = new ConcreteBuilding(); // object created
building.openDoor(); // uses implemented method
building.turnOnLights(); // uses its own method
}
}Output:
Door opened in the building.Lights are turned on.5. POJO Class
POJO stands for "Plain Old Java Object"
A POJO class is a simple data container with private variables and public getter and setter methods. It does not have its own behavior, but it can override methods like equals() or implement interfaces like Serializable.
Properties of a POJO Class:
- Use private variables for all data
- Provide public getter and setter methods
- Do not extend other classes
- Do not implement pre-defined interfaces
- Do not use pre-defined annotations
- Should have a default no-argument constructor
// POJO class
public class Building {
// private variables
private String name;
private int floors;
private String color;
// No-argument constructor
public Building() {}
// Getter and Setter for name
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// Getter and Setter for floors
public int getFloors() {
return floors;
}
public void setFloors(int floors) {
this.floors = floors;
}
// Getter and Setter for color
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}6. Singleton Class
A singleton class allows only one object to exist at a time.
Key Concepts:
- If you try to create another object, it refers to the same existing object
- Any changes made through one instance affect the single object
- Commonly used for database connections or socket programming
How to Create a Singleton Class:
- Make the constructor private so no other class can create an object
- Provide a static method that returns the single instance (lazy initialization)
class Building {
// static variable to hold single instance
private static Building singleInstance;
// private constructor so no other class can create object
private Building() {}
// static method to provide single instance
public static Building getInstance() {
if (singleInstance == null) {
singleInstance = new Building(); // create only once
}
return singleInstance;
}
// example method
public void showInfo() {
System.out.println("This is the only Building instance.");
}
}
public class Main {
public static void main(String[] args) {
Building b1 = Building.getInstance();
Building b2 = Building.getInstance();
b1.showInfo();
b2.showInfo();
// Check if both references point to the same object
if (b1 == b2) {
System.out.println("Both b1 and b2 refer to the same instance.");
}
}
}Output:
This is the only Building instance.This is the only Building instance.Both b1 and b2 refer to the same instance.7. Inner Class
An inner class is a class defined inside another class in Java.
Benefits:
- Helps to organize classes logically
- Achieves better encapsulation
- The inner class can access all members of the outer class, including private ones
class Building {
private String name = "Sky Tower";
// Inner class
class Room {
void showRoomInfo() {
// Inner class can access outer class's private members
System.out.println("This room is in the building: " + name);
}
}
}
public class Main {
public static void main(String[] args) {
Building building = new Building();
// Create object of inner class
Building.Room room = building.new Room();
room.showRoomInfo();
}
}Output:
This room is in the building: Sky TowerQuick Reference Summary
| Class Type | Key Feature | Use Case |
|---|---|---|
| Final | Cannot be inherited | Immutable classes |
| Static | Nested, no outer object needed | Grouping related classes |
| Abstract | Has abstract methods | Template for subclasses |
| Concrete | Fully implemented | Working, instantiable classes |
| POJO | Simple data container | Data transfer objects |
| Singleton | Only one instance | Database connections |
| Inner | Defined inside another class | Logical grouping, encapsulation |
