final Keyword Java Examples Screenshots

final Keyword Java Examples Screenshots

final Keyword Java

After going through "static" and "super" keywords, let us go another keyword "final". These three keywords, known as access modifiers, are used very often in Java code. The final keyword behaves very differently when applied to variables, methods and classes. Even though its functionality (purpose) is different in the three contexts, but one thing is common; it does not allow the programmer to do certain actions.

  1. A final variable cannot be reassigned.
  2. A final method of the super class cannot be overridden by subclass
  3. A final class cannot be extended by any other class. Final class does not fit for inheritance.

Three programs are given to explain the above three rules.

1. final Keyword with Variables

"const" keyword of C/C++ is not supported by Java and in its place, Java uses "final" keyword. That is, a final variable cannot be reassigned. Once assigned, cannot be modified later.

final Keyword Java

Output screen of Book.java

In the above code, the variable cost is declared as non-final and packing as final. The cost variable is reassigned to 500 but packing cannot be(if the comments are removed in the code, it is compilation error). After comfortable with final variables let us go to final methods.

2. final Keyword with Methods

In inheritance, the subclass is at liberty to use super class methods or override and use its own methods. Moreover, with "super" keyword, it can make use of its own and that of super class also. This is possible as long as super class permits. We know earlier subclass is rich; but now I say one more "super class is powerful". Subclass can play with super class as long super class permits. At any time, super class can place some restrictions on subclass about it' members usage.

Now the super class would like to place a small restriction on subclass; it says, few methods of it's cannot be overridden. The super class declares those methods as final. That is, final methods of the super class cannot be overridden.

final Keyword Java

Output screen of Peacock.java

In Bird's class, eat() method is declared as final and fly() as non-final. The eat() method cannot be overridden by the subclass Peacock and regarding fly() method, subclass is at liberty to override (method overriding). In the above code, it is overridden. Infact, the subclass can call super class fly() method also with super.fly() (not used in the above code).

3. final Keyword with Classes

In inheritance, a class can be extended by another class; but it is at the mercy of the super class. If the super class does not like any one to inherit, simply it declares its class as "final". A final class cannot be extended by any other class. A final class does not fit for inheritance.

final Keyword Java

Output screen of Aeroplane.java

Spacecraft class is declared as final and for this reason, Aeroplane cannot extend it. But Aeroplane class can make use of final class methods by "composition".

8 thoughts on “final Keyword Java Examples Screenshots

  1. mohit

    sir,
    somewhere i read ..
    Parent p = new Child();( refferential polymorphism )
    i had put above statement n came to know that results were no different than
    Child p = new Child(); ( compile time polymorphism )
    as in both cases it will show non overridden methods from parent and overridden methods from child , is there any difference or benefit of using run time polymorphism over compile time polymorphism ?

      1. mohit

        sir, but i m not able to think of any scenario where these two ways hold different meaning or what advantege other has over first one , can you please explain me with an example or anything .
        Thanks .

          1. vinod chahar

            final work in three way-
            1-final variable initialized at declared time.
            2-initial by block(init block or static block at once in program)
            3-by the help of constructor

            sir i am little confusion final variable can’t have default value by JVM that called blank final.
            but in my program why JVM initialized final variable by default value.

            class test
            {
            static final int x=m();
            static int m()
            {
            System.out.println(“x= “+x);
            return 100;
            }
            public static void main(String args[])
            {
            System.out.println(“x= “+x);
            }
            }

            output:-
            x=0
            x=100

  2. surya

    sir,

    what is the need of overriding.??? instead of override why can’t we declare directly in superclass itself what we wants.

    could u please explain…???

Leave a Reply

Your email address will not be published. Required fields are marked *