Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Java For Automation

  • What is Java?
  • Why to Learn Java?
  • History of Java
  • Types of Java Applications
  • Naming Conventions
  • Hello World Program
  • Internal Details of Java program
  • What is Java JDK, JRE and JVM ?
  • What is Java Comments ?
  • What is Java Variable?
  • Variable Naming convention
  • Primitive Data Types
  • Non Primitive Data Types
  • Type Casting
  • Java Arithmetic Operators
  • Relational Operators
  • What is logical operators in Java? 
  • What is Assignment Operators in Java? 
  • What is Unary Operators in Java? 
  • What is Ternary Operators in Java?
  • What is Java Control Statements?
  • What are Decision Making statements?
  • What are looping statements?
  • What are Jump statements?
  • What is OOPS concept in Java?
  • Advantages of Java OOPs
  • What is Java Inheritance?
  • What is Java Constructor?
  • What is this keyword?
  • What is Polymorphism?
  • What is super keyword ?
  • What is final keyword? 
  • What are access modifiers?
  • What is Encapsulation? 
  • What is Abstraction? 
View Categories
  • Home
  • Tutorials
  • Java
  • Java For Automation
  • What is final keyword? 

What is final keyword? 

What is final keyword? 

The final keyword in Java is used to define constants, prevent method overriding, and prevent inheritance of classes. It can be applied to variables, methods, and classes, and its behavior varies depending on where it is used.

Uses of final Keyword #

  1. Final Variables
  2. Final Methods
  3. Final Classes

1. Final Variables #

Definition: When a variable is declared as final, its value cannot be changed once it has been initialized. This makes the variable a constant.

Example:

class Constants {

    // Final variable
    final int MAX_VALUE = 100;

    void display() {

        System.out.println("Maximum Value: " + MAX_VALUE);

    }

}
public class Main {

    public static void main(String[] args) {

        Constants c = new Constants();

        c.display();

        // Trying to change the value will cause a compile-time error

        // c.MAX_VALUE = 200; // Uncommenting this line will cause a compilation error

    }

}

In this example, MAX_VALUE is a final variable, meaning its value cannot be modified after it is initialized.

2. Final Methods #

Definition: A final method cannot be overridden by subclasses. This ensures that the method’s implementation remains unchanged.

Example:

class Animal {

    // Final method

    final void eat() {

        System.out.println("Animal eats food.");

    }

}

class Dog extends Animal {

    // Trying to override this method will cause a compile-time error

    // void eat() { ... } // Uncommenting this line will cause a compilation error

}
public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();

        d.eat(); // Outputs: Animal eats food.

    }

}

In this example, the eat method in Animal is final, so it cannot be overridden by the Dog class.

3. Final Classes #

Definition: A final class cannot be subclassed. This means no other class can extend a final class.

Example:

// Final class

final class FinalClass {

    void display() {

        System.out.println("This is a final class.");

    }

}

// Trying to extend a final class will cause a compile-time error

// class SubClass extends FinalClass { ... } // Uncommenting this line will cause a compilation error

public class Main {

    public static void main(String[] args) {

        FinalClass fc = new FinalClass();

        fc.display(); // Outputs: This is a final class.

    }

}

In this example, FinalClass is a final class, so it cannot be extended by any other class.

Summary #

  • Final Variables: Once initialized, their value cannot be changed.
  • Final Methods: Cannot be overridden by subclasses.
  • Final Classes: Cannot be subclassed.

The final keyword helps enforce immutability, ensure consistent behavior, and prevent unintended modifications or extensions in your Java programs.

Java
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
What is super keyword ?What are access modifiers?
Table of Contents
  • Uses of final Keyword
  • 1. Final Variables
  • 2. Final Methods
  • 3. Final Classes
  • Summary
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105