1

I was reading through The Java Programming Language, Fourth Edition, and came across some confusing statements on synchronization in the "Threads" chapter. The information is as follows:

The way that synchronization is enforced in a class is an implementation detail. The fact that it is enforced is an important part of the contract of the class and must be clearly documented--the precense of the synchronized modifier on a method might only be an implementation detail of the class, not part of a binding contract.

Correct me if I'm wrong, but it makes sense to me that there could be an un-synchronized method that is then overloaded in a subclass's implementation of that method such that it is synchronized.

However...

If a class's method is declared synchronized, can that method be overridden (or is it overloading in this case?) in a subclass such that it is the exact same but without a synchronized clause? If not, are there any ways to override/overload a method in a subclass such that it is not synchronized when the superclasses implementation is?

4
  • Does JLS-17.1. Synchronization help clarify what the book is saying? The distinction being on whether it is a synchronized statement (§14.19) or a synchronized method (§8.4.3.6). Commented Nov 28, 2015 at 0:37
  • See also: stackoverflow.com/questions/10173345/… Commented Nov 28, 2015 at 0:43
  • If a sub-class overrides a method that is synchornized in the super-class but does not add the synchronized modifier itself, the sub-class method is not synchronized. If the sub-class method calls super.method() then the super-class method is still synchronized. Commented Nov 28, 2015 at 0:43
  • @ElliottFrisch I don't see anything explicit in the JLS sections you cite that says whether synchronized is or is not a heritable method attribute. It's implied though by the examples in 8.4.3.6 which proclaim that a synchronized method "has exactly the same effect as" a method whose entire body is a single synchronized block. Method bodies obviously are not inherited. Commented Nov 29, 2015 at 11:14

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.