Can anybody explain why we cannot declare a synchronized method in an interface, by giving a real world example.
Asked
Active
Viewed 906 times
1 Answers
3
The answer is simple, a synchronized is an implementation detail and it does not belong to an interface. In an interface all the methods do not have the implementation detail.
The docs says:
Note that a method declared in an interface must not be declared strictfp or native or synchronized, or a compile-time error occurs, because those keywords describe implementation properties rather than interface properties. However, a method declared in an interface may be implemented by a method that is declared strictfp or native or synchronized in a class that implements the interface.
You may also refer: What is the reason why “synchronized” is not allowed in Java 8 interface methods?

Community
- 1
- 1

Rahul Tripathi
- 168,305
- 31
- 280
- 331
-
Then, should not we put in the interface but the implementor class? – wolfenblut Dec 20 '22 at 16:51