Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract method and synchronized clause

 
Rhea Karnam
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I read that an abstract method cannot be final, static, native or synchronized.
Can anyone help me in understanding why an abstract method cannot be declared as synchronized?
In the following snippet, declaring the method as synchronized in class A turned out to be illegal. But, when I qualify the same method as synchronized in the child class, I don't get any compile time errors:
abstract class A
{
abstract void method1();
}
class B extends A
{
public synchronized void method1()
{
}
}
I expected an error because I thought that I can't alter the signature of an abstract method, while implementing it. Can somebody throw light on this?
Regards,
Rhea
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good question; I tested this on both Windows and Linux and the behavior was as you indicated on both platforms. I looked through the Java language specification and it said that abstract methods can't be "private", "static" or "final", but it didn't say anything about "synchronized." However the Java VM spec did add "synchronized" and "strictfp" as forbidden modifiers on abstract methods.
My only guess is that since "synchronized" methods attempt to lock the instance, and abstract classes can't be instantiated, that there isn't any object to lock.
In terms of altering the signature, there are rules in terms of visibility (private/public/protected/[none]) and what exceptions it throws, but not in terms of "final" or "synchronized", since they don't affect the visibility of the method.
[ September 30, 2003: Message edited by: Wayne L Johnson ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic