• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

abstract method and synchronized clause

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
I don't always make ads but when I do they're tiny
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic