Java final Keyword (final variable, final method, final class)

Java final Keyword (final variable, final method, final class) - With Examples

Quick Summary

Further, in this post, you will get to know about the concept and uses of the final keyword along with its subparts, i.e., final variable, final method, and final class, and how it is helpful to make Java code more secure.


Java Final Keyword OOP Interview In Java, certain things are best left unchanged once they are created. For instance, a constant data item such as PI, or a method that is not intended to be overridden. That’s when the final keyword is extremely handy.


1) What is final Keyword in Java?

The final keyword in Java is used to restrict something. It can be applied to:

  • Variable → value cannot be changed
  • Method → method cannot be overridden
  • Class → class cannot be inherited

✅ Simple meaning: final = "don’t change this"


2) final Variable in Java

A final variable is a variable whose value cannot be changed once assigned. It is used to create constants.


class FinalVariableExample {

    public static void main(String[] args) {

        final int age = 22;
        System.out.println("Age: " + age);

        // age = 25;  ❌ ERROR: Cannot assign a value to final variable
    }
}
Output:

⚠️ Rule: A final variable must be assigned only once.


3) final Variable with Object Reference

This is a very common interview confusion. When you make an object reference final, you cannot change the reference, but you can still change the object data (if it is not immutable).


class Student {
    String name;

    Student(String name) {
        this.name = name;
    }
}

public class FinalObjectExample {

    public static void main(String[] args) {

        final Student s1 = new Student("Aishwarya");

        // Allowed (changing object data)
        s1.name = "Aishwarya";

        System.out.println("Student Name: " + s1.name);

        // Not allowed (changing reference)
        // s1 = new Student("New Student"); ❌ ERROR
    }
}
Output:


4) final Method in Java

A final method is a method that cannot be overridden in the child class. This is useful when you want to keep logic fixed and secure.


class Parent {

    final void show() {
        System.out.println("This is a final method from Parent");
    }
}

class Child extends Parent {

    // ❌ ERROR: Cannot override the final method
    // void show() {
    //     System.out.println("Overriding final method");
    // }
}

public class FinalMethodExample {
    public static void main(String[] args) {
        Child c = new Child();
        c.show();
    }
}
Output:


5) final Class in Java

A final class is a class that cannot be inherited. This is useful for security and to prevent modification.


final class Demo {

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

// ❌ ERROR: Cannot inherit from final class
// class Test extends Demo {
// }

public class FinalClassExample {
    public static void main(String[] args) {
        Demo d = new Demo();
        d.display();
    }
}
Output:


6) Final Keyword Summary Table

Where used Meaning Restriction
final variable Constant value Cannot change value
final method Fixed logic Cannot override
final class Secure class Cannot inherit

Developer Tip

Developer Tip: Always use final for constant values like: PI, MAX_LIMIT, DEFAULT_TIMEOUT. It makes your code cleaner, safer, and more professional.


Interview Notes (Very Important)

  • final variable cannot be reassigned
  • final method cannot be overridden
  • final class cannot be inherited
  • ⚠️ final object reference cannot change reference, but object data can change

FAQ (Frequently Asked Questions)

Q1) Can we overload a final method?

✅ Yes. Overloading is allowed because it is the same class, not overriding.


Q2) Can we override a final method?

❌ No. Java does not allow overriding a final method.


Q3) Can a constructor be final?

❌ No. Constructors cannot be final because they are never inherited.


Q4) Why is String class final in Java?

Because Java wants String to be secure and immutable. If it was not final, someone could override it and break security.


Conclusion

The final keyword is among the key Java keywords that can be used for effective, uncorrupted, and reliable programming. This keyword is used in actual projects.

✅ If you are giving interviews, you MUST remember; final variable, final method, and final class.


Next Post: Java abstract Keyword (abstract class, abstract method) - With Examples


Labels: Java, final keyword, final variable, final method, final class, OOP, Java interview questions

Comments

Popular posts from this blog

Inheritance in Java (Single, Multilevel, Hierarchical)

The Java Blueprint: Your First Step into Software Development

Java Variables and Data Types: Storing Your Data