Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces and synchronized keyword

 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why is it that the method1 under TwoInterfaces doesn't give a compiler error when I added to it a synchronized modifier?
 
Kalyani Marathe
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classes implementing interfaces are required to have public method and then you can use any other modifier to it like synchronized or native.
 
Paulo Aquino
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah...thanks...but you can't use native here because native must not have any body right?
 
Francisco Gonzalez
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually you can try it and see

-Francisco
 
Francisco Gonzalez
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add some more.

when you're implementing an interface, what you're basically doing is overriding the methods defined in that interface. So all overriding rules apply, which does not restrict you from use modifiers such as: synchronized and native even abstract if it's an abstract class that implements the interface.

You need to keep the same signature, the access modifier can't be more restrictive, do not throw any new or broader checked exception, etc,etc

Regards,
Francisco.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic