File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes abstract method and synchronized clause Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "abstract method and synchronized clause" Watch "abstract method and synchronized clause" New topic

abstract method and synchronized clause

Rhea Karnam

Joined: May 06, 2003
Posts: 21
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?
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
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 ]
I agree. Here's the link:
subject: abstract method and synchronized clause
It's not a secret anymore!