Is it "ok" to add the final keyword to an inherited/overridden method?

android, inheritance, java

Solution

Yes, technically it's a clean solution. If you are absolutely sure there is no situation when someone might want to change any logic inside this particular method you could do that.

Also, you have to remember that this method has to contain the least logic possible, as it can not be altered by any descendant. If you think this method will grow in size (without using delegates) avoid finalizing it.

Problem

In Android i create an abstract class that extends `View` (an Android class to which i have no access). The abstract class overrides the Views ``` @Override protected final void onDraw(Canvas canvas) { if(conditions) return; // child classes should only draw if this class gives the ok subDraw(canvas); } protected abstract void subDraw(Canvas canvas); ``` however i added the final keyword here. The point is, i create an abstract method that the subclasses are supposed to use instead of the onDraw. So i prevent the onDraw from being overridden any further and it works. I know there are designs to make this better, however this works like a charm without a lot of change. My question is more in general if doing the above has unwanted side effects at runtime or other issues ?!

Original source