Why we cannot use synchronized keyword in an interface method declaration

957 views Asked by At

Can anybody explain why we cannot declare a synchronized method in an interface, by giving a real world example.

1

There are 1 answers

1
Rahul Tripathi On BEST ANSWER

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?