• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

abstract synchronized methods not allowed.

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mark Gabb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mark Gabb
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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. :-)
 
reply
    Bookmark Topic Watch Topic
  • New Topic