• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronised Overidden  RSS feed

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a Synchronised method be overridden by child class's method ? if yes what would be the signature of that method ? Will it be necessarily synchronised method ?
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The signature of a method is completely independant of whether it's synchronized or not. A synchronized method can override a non-synchronized method, or vice versa. The compiler doesn't care. The only manner in whihc synchronization is relevant to overriding method is this: a javadoc comment can say anything you want it to, and overriding methods are (let's say) morally obligated to follow whatever specifications the API lays out. So if the javadoc API for a method says "this method is guaranteed to work consistently if called from multiple threads" then you may need to make the overriding method synchronized in order to meet the obligation which has been conferred on you. However, the compiler doesn't care, since it doesn't understand English, and doesn't read javadoc comments. As far as the compiler is concerned, javadoc comments are irrelevant, and you are allowed to make a method synchronized, or not. Either way, the compiler will not complain. So yes, it's possible that if you're overriding a synchonized method you should synchronize the overriding method as well. But that's not enforced by the compiler; it's enforced by humans who read javadoc comments and interpret them.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now try and tell me that the compiler doesn't care about javadoc comments
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
So if the javadoc API for a method says "this method is guaranteed to work consistently if called from multiple threads" then you may need to make the overriding method synchronized in order to meet the obligation which has been conferred on you.

Also notice that a method *can* be thread-safe without being synchronized.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!