• 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

Interfaces with static and synchronized methods

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've seen some different explanations on why interfaces cannot have static methods nor synchronized methods but none of them have sounded convincing. Does anyone know the definitive reasons why an interface cannot have static methods nor synchronized methods? Thanks.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, I do not have the definitive answer, but I think that I have a good one.
As you know, the JLS simply states that static methods can not be abstract. If I had to speculate on Sun's reasoning for that design choice, I would say that it is because a subclass does not override a static method declared in a super class. Instead, a subclass only hides a super class method. The differences between overriding and hiding are probably the motivation for the restriction.
The reference to a static method is resolved at compile time based on the type of the reference. If the reference has the type of the base class, then the base class method will be invoked at run time even if the reference object has a sub class type that hides the base class method. If the base class method is abstract, then the method invoked at runtime would have no implementation.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that the following question from my mock exam does a good job of illustrating the differences between method overriding and method hiding.

The correct answer is b. Class Q hides printS1 and overrides printS2. Therefore, the superclass implementation of printS1 is invoked and the sub class implementation of printS2 is invoked.
If P.printS2 had been abstract, then the program would still work. However, if the static method P.printS1 had been abstract, then the invoked method would have no implementation.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronized methods are not allowed to be declared within an interface just because synchronization is an implementation issue. Since a method is not implemented in an interface there is no reason to put that keyword there.
From JLS 9.4 Abstract Method Declarations


Note that a method declared in an interface must not be declared strictfp or native or synchronized, or a compile-time error occurs, because those keywords describe implementation properties rather than interface properties. However, a method declared in an interface may be implemented by a method that is declared strictfp or native or synchronized in a class that implements the interface.

 
Ricardo Cortes
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this on Jguru.com. Here's the link to the same qusetion:
http://www.jguru.com/faq/view.jsp?EID=88225
Do you agree with the answer?
Darren Abbruzzese, Oct 10, 2001


The reason why you can't have a static method in an interface lies in the way Java resolves static references. Java will not bother looking for an instance of a class when attempting to execute a static method. This is because static methods are not instance dependent and hence can be executed straight from the class file. Given that all methods in an interface are abstract, the VM would have to look for a particular implementation of the interface in order to find the code behind the static method so that it could be executed. This then contradicts how static method resolution works and would introduce an inconsistency into the language.

 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good explanation indeed
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic