Can I make an overridden method synchronized?

I am redefining a method from a superclass, however I want this method to be synchronized. It is allowed? What could be an alternative?

+7
java synchronized method-overriding
source share
1 answer

Yes, this is allowed, because it does not change the contract, but implements it.

Think that you can always just add a synchronized block:

synchronized(this) { 

only at the beginning of a method that will achieve roughly the same result. There may also be other (possibly hidden) locks in the method, which makes this really part of the implementation, not the API.

+8
source share

All Articles