Removing final keyword from an existing method

Viewed 373

Say, I have an existing code, which looks somewhat like this:

public class MyClass {
    private AnotherClass obj;
    // ... (class implementation)
    public final getObj() { return obj; // It is this simple }
}  

This class is implemented in a library, the versions of which are used by many different code bases.

I would like to remove the final from the getObj() method definition. I am aware of the fact that the method will no longer be inlined.

What other possible side effects could removing the final keyword from getObj() cause to its existing users?

2 Answers

As @Khelwood commented:

When a method is declared with final keyword, it is called a final method and it cannot be overridden and We must declare methods with final keyword for which we required to follow the same implementation throughout all the derived classes.

So, if you remove the final keyword any implementation class can override this method functionality (as @Khelwood suggestion it is better to know why it was declared as final before removing it).

From your source:

Methods that can be inlined include static, private or final methods but also public methods if it can be determined that they are not overridden.

Inlining is a process to minimize the overhead, and methods are inlined depending on the number of loops and calls.

The final keyword is used to define an entity that can only be assigned once. When used with methods they can not be overridden.

So removing it, means only you can assign that variable to another object, in case it is a method they can be overridden when the interface implemented or when the class inherited, no other side-effect!

Related