OK to add the last keyword to the inherited / overridden method?

In Android, I create an abstract class that extends View (an Android class that I don't have access to). Abstract class overrides 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 last keyword here.

The fact is that I am creating an abstract method that is supposed to use subclasses instead of onDraw. Therefore, I prevent overriding onDraw further and works.

I know that there are projects to make it better, however it works like a charm without big changes. My question is bigger in the general case if, when doing the above, there are undesirable side effects at runtime or other problems ?!

+6
source share
1 answer

Yes, technically this is a clean solution. If you are absolutely sure that there is no situation where someone might want to change some logic in this particular method, you can do it.

In addition, you should remember that this method should contain the least logic, since it cannot be modified by any descendant. If you think this method will grow in size (without the use of delegates), do not complete it.

+2
source

Source: https://habr.com/ru/post/1011767/


All Articles