AI May 15, 2023 โ€ข 5 min read

Module 29: Final Keyword

BR

Bhargava Ram Pantina

Web Developer & AI Enthusiast

๐Ÿ”’ What is the final Keyword?

The final keyword in Java is used to restrict the user. It can be applied to variables, methods, and classes.

๐Ÿ”น Usage of final

  • Final Variable: The value cannot be changed once initialized.
  • Final Method: Cannot be overridden by subclasses.
  • Final Class: Cannot be extended (inherited).

๐Ÿงช Examples

                    final class Vehicle {
                      void display() {
                        System.out.println("This is a vehicle");
                      }
                    }
                    
                    class Car /* extends Vehicle */ {  // โŒ Error: Cannot inherit from final class
                    }
                    
                    class Animal {
                      final void sound() {
                        System.out.println("Animal makes a sound");
                      }
                    }
                    
                    class Dog extends Animal {
                      // void sound() {}  // โŒ Error: Cannot override final method
                    }
                    
                    public class Main {
                      public static void main(String[] args) {
                        final int speed = 90;
                        // speed = 100;  // โŒ Error: Cannot assign a new value to final variable
                        System.out.println("Speed: " + speed);
                      }
                    }

๐ŸŽฏ Why Use final?

  • To prevent value modification (constants)
  • To secure method behavior from changes
  • To prevent inheritance of sensitive classes
Artificial Intelligence Web Development Technology Trends Machine Learning UX Design

Share this article

About the Author

BR

Bhargava Ram Pantina

Bhargava Ram is a web developer and AI enthusiast with over 8 years of experience in the tech industry. He specializes in creating innovative web solutions that leverage the latest technologies to solve real-world problems.

Related Articles

AI Mar 15, 2023

Implementing ChatGPT in Your Web Application: A Step-by-Step Tutorial

Learn how to integrate OpenAI's ChatGPT into your web application to create intelligent chatbots and enhance user interactions.

Read More โ†’
Web Development Apr 22, 2023

Building Accessible Web Forms: A Complete Guide

Learn how to create web forms that are accessible to all users, including those with disabilities. This comprehensive guide covers ARIA attributes, keyboard navigation, and more.

Read More โ†’

Subscribe to My Newsletter

Get the latest articles and resources delivered straight to your inbox.

No spam, just valuable content. Unsubscribe anytime.