Java Final Keyword Explained

🚀 30 Days of Java Interview Questions – Day 12 💡 Question: What is the final keyword in Java? 🔹 What is final? final is a keyword in Java used to restrict modification. It can be applied to: • Variables • Methods • Classes 🔹 final Variable Once a variable is assigned, its value cannot be changed. Example: ```java id="k3h9ds" final int x = 10; // x = 20; ❌ Error ``` 🔹 final Method A final method cannot be overridden in a subclass. ```java id="p8f2la" class A { final void show() { System.out.println("Hello"); } } ``` 🔹 final Class A final class cannot be extended (no inheritance allowed). Example: ```java id="z1d8qp" final class A {} // class B extends A ❌ Not allowed ``` ⚡ Quick Summary • final variable → value cannot change • final method → cannot override • final class → cannot inherit 📌 Interview Tip final is widely used to create immutable objects and to improve security and performance in Java applications. Follow this series for 30 Days of Java Interview Questions. #java #javadeveloper #codinginterview #backenddeveloper #softwareengineer #programming #developers #tech

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories