Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java final class
Prevent Inheritance



In Java, the final keyword when used with a class, it signals that the class cannot be subclassed or extended.

This concept is especially valuable when you want to design components that are not meant to be changed through inheritance — for example, utility or security-related classes.

Why use a final class?

Marking a class as final is a purposeful decision. Here’s why developers often do it:

Syntax of final class

final class Vehicle {
    void displayType() {
        System.out.println("This is a vehicle.");
    }
}

Here, the Vehicle class is declared final, which means:

Attempting to extend a final class (Compilation Error)

final class Vehicle {
    void displayType() {
        System.out.println("This is a vehicle.");
    }
}

class Car extends Vehicle { // ❌ Compilation Error
    void showModel() {
        System.out.println("This is a sedan.");
    }
}
Error: Cannot inherit from final 'Vehicle'

Valid usage of a final class

You can still create objects and call its methods as usual:

final class Logger {
    void log(String message) {
        System.out.println("LOG: " + message);
    }
}

public class Main {
    public static void main(String[] args) {
        Logger logger = new Logger();
        logger.log("Application started");
    }
}
LOG: Application started

Common real-world examples of final classes

Java itself defines several final classes:

These classes are final because altering their behavior could break Java’s core functionality or create serious security vulnerabilities.

When should you declare a class as final?

Use final when:

Things to remember

Can a final class have final methods?

Yes! In fact, you can declare methods as final to prevent them from being overridden in subclasses (though in a final class, overriding is impossible anyway).

final class Shape {
    final void draw() {
        System.out.println("Drawing shape...");
    }
}

This is often done to document intention — even if inheritance is blocked, the developer is being explicit that the method shouldn't be touched.

Conclusion

The final keyword, when applied to classes, enforces boundaries, supports clean API design, and aligns with object-oriented principles like encapsulation and immutability.

While it may seem restrictive at first glance, it encourages you to write intentional, safe, and maintainable code.

QUIZ

Question 1:What does marking a class as final in Java achieve?

Question 2:A final class in Java cannot have methods that are overridden by subclasses.

Question 3:What happens when you try to compile the following code?
final class Vehicle {
    void showType() {
        System.out.println("Vehicle");
    }
}

class Car extends Vehicle {
    void showModel() {
        System.out.println("Sedan");
    }
}

Question 4:Which of the following are valid reasons to declare a class as final in Java?

Question 5:Declaring a class as final in Java automatically makes all its fields final too.

Question 6:Which of the following is a valid use case for final classes in real-world Java applications?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M