File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract synchronized methods not allowed. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract synchronized methods not allowed." Watch "abstract synchronized methods not allowed." New topic
Author

abstract synchronized methods not allowed.

Mark Gabb
Greenhorn

Joined: Feb 03, 2008
Posts: 21
Hi all,

Could anyone give me any information on why you can't have an abstract synchronized method like below:
I can't see why you can't do this, and the compiler is not giving me any information?
Also I can't find any useful information on google.



Cheers

Mark
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

A thread which enters the synchronized method must get the lock of the object (or of the class) in which the method is defined. You can not instantiate an abstract class so there is no object with the lock.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Mark Gabb
Greenhorn

Joined: Feb 03, 2008
Posts: 21
Ahhh, I see :-)

I have just realised now that you can override a synchronized method with a non-synchronized version of the method so this would make sense.

Thank you for your help Lukas.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

You can also view it like this: Whether a method needs to be synchronized, really depends on the implementation of the method; it's an implementation detail. An abstract class only declares the interface of abstract methods, and doesn't know anything about the implementation (which is in a concrete subclass of the abstract class). It would be strange if an implementation detail is specified in the interface of a method.

You can use the 'synchronized' modifier on a concrete implementation of an abstract method, which is where it belongs.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mark Gabb
Greenhorn

Joined: Feb 03, 2008
Posts: 21
Jesper Young wrote:You can also view it like this: Whether a method needs to be synchronized, really depends on the implementation of the method; it's an implementation detail. An abstract class only declares the interface of abstract methods, and doesn't know anything about the implementation (which is in a concrete subclass of the abstract class). It would be strange if an implementation detail is specified in the interface of a method.

You can use the 'synchronized' modifier on a concrete implementation of an abstract method, which is where it belongs.


Ok, thank you. :-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: abstract synchronized methods not allowed.
 
Similar Threads
Abstract methods
modifier
Regarding Interface
a question about modifier
abstract method and synchronized clause