2

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

Aqeel Ashiq
  • 1,988
  • 5
  • 24
  • 57

1 Answers1

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