Using Final to Prevent Overriding Assignment Help

Assignment Help: >> Inheritance in Java - Using Final to Prevent Overriding

Using final to Prevent Overriding

Although method overriding is one of Java's most powerful characteristic, there will be times whenever you will need to avoid it from occurring. For disallow a method from being overridden and specify  final  as  a  modifier  at  the  begin  of  its  declaration.  A Method declared as final cannot be overridden. The subsequent fragment describes final:

class A{

final void meth( ) {

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

}

}

class B extends A {

void meth( ) { // ERROR! Can't override.

System.out.println(("Illegal!");

}

}

Since meth ( ) is declared as final, it cannot be overridden in B. a compile-time error will result if you attempt to do so.

Methods declared as final could sometimes give a performance enhancement. The compiler is free to inline calls to them since it "knows" they will not be overridden through a subclass. Whenever a small final function is called frequently the Java compiler can copy the byte code for the subroutine directly inline along with the compiled code of the calling method, therefore eliminating the costly overhead related along with a method call. Inlining is only an option that is used with final methods. Commonly, Java resolves calls to methods dynamically at run time. This is known as late binding. However because final methods cannot be overridden a call to one can be resolved at compile time. This is known as entry binding.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd