by Dinesh Thakur

The final keyword has the following uses in the inheritance.

        If you want the method must not be overridden then defined that method as final.

 

Example:

 

class Abc

{

final public void dispaly()

{

System.out.println("I am in Abc");

}

}

class Xyz extends Abc

{

public void dispaly()

{

System.out.println("I am in Xyz");

}

}

 

At the time of compiler will show the error message given in the image.

                           Use of 'FINAL' Keyword

If you want to prevent a class from being inherited then declare that as final.

 

Example:

 

final class Abc

{

public void dispaly()

{

System.out.println("I am in Abc");

}

}

class Xyz extends Abc

{

public void dispaly()

{

System.out.println("I am in Xyz");

}

}

 

At the time of compiler will show the error message given in the image.

                         Final Keyword Error

If you want to create a constant then also use final with the variable declaration.

The methods declare as final, compiler is free to make inline calls for those methods. If a small final method is called, the compiler copies the bytecode of the final method as inline code to the calling method. The final method calls are resolved at compile time called, early binding.