Search

Sunday, May 23, 2021

Java Final Keyword

 

Java Final Keyword

Java final keyword. If you want to get knowledge and clear understanding about use of final keyword in Java. Then its a best place for you, here i will teach you in detail about final keyword with very easy to understand examples.


Use of Final Keyword in Java

Sometimes in Java language, we need to declare a variable with a value which we do not want to change. We want it to remain fix all the time. We can do this using final keyword. This is usually used to make constants. This final keyword can be used in Java with variables or methods or even with classes. If we define any variable or method or class with final keyword, it means that they can be assigned only one time. So variables with final keyword cannot be initialized again with some other value.

If we use final keyword with some Java methods, it means they cannot be override. So if we do not want some method to be overridden, we can use this final keyword to stop method overriding in Java. Also if we want to stop inheritance, i.e. if we do not want that some class can be inherited, then we can use this final keyword with a class, which means that final class cannot be inherited.


Final Variable Example Program

class Test

{

                public static void main (String ar[ ])

                {

                                final int a = 1;

                                a = 10;

                                System.out.println(a);

                }

}


In the above program, we have a variable and we made it final to make it constant and in the next line, we are trying to change the value of that constant and that is the reason we will get erro because we cannot change value of those variables which are define using final keyword.

 

Final Method Example Program


class A

{

                public final void method1()

                {

                System.out.println(“final method”);

                }

}


class B extends A

{

                public final void method1()

                {

                System.out.println(“overriding”);

                }

                public static void main (String ar[ ])

                {

                B obj = new B();

                obj.method1();

                }

}


In the above program, we have two classes, class A and class B. Class B is trying to inherit Class A. Inside Class A, we have a method known as method1. This method is defined using the final keyword and because of this, it cannot be override because, using final keyword, we can stop overriding the method in inheritance. But Class B is trying to override this method and that is the reason, we will get error in this program.

 

Final Class Example Program

final class A

{

                public void method()

                {

                System.out.println(“final class method”);

                }

}


class B extends A

{

public void method()

                {

                System.out.println(“overriding”);

                }

                public static void main (String ar[ ])

                {

                 B obj = new B();

                obj.method();

                }

}


In the above program, we will get error, because we cannot inherit final class in Java and here Class A is a final class which is inherited by the Class B.


No comments:

Post a Comment