• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Static methods Vs default methods in implementing class

 
Greenhorn
Posts: 4
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does this compile? Am I not trying to override a static method here ?



As this does not compile.

 
Bartender
Posts: 15737
368
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is because of the history of the Java language.

Static methods in interfaces were only added in Java 8. By that time, many classes were written that implement existing interfaces. When the developers allowed for static methods to be added to interfaces, they wanted to make sure that existing classes didn't suddenly break because the interface that they implemented had added a static method that happens to have the same name as a method in the class.

If interfaces had supported static methods from the very beginning, the first snippet of code probably would have caused a compilation error as well.
 
Nation Chirara
Greenhorn
Posts: 4
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:The reason is because of the history of the Java language.

Static methods in interfaces were only added in Java 8. By that time, many classes were written that implement existing interfaces. When the developers allowed for static methods to be added to interfaces, they wanted to make sure that existing classes didn't suddenly break because the interface that they implemented had added a static method that happens to have the same name as a method in the class.

If interfaces had supported static methods from the very beginning, the first snippet of code probably would have caused a compilation error as well.



This drove the point home, I got you thanks!
 
Bartender
Posts: 5584
213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is one little snag, though.

Normally we say that you must program to the interface, like: List<...> list = new ArrayList<>();
But if you do that here:

then b.m1() is illegal.
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:then b.m1() is illegal.


If you program to the interface, you won't need an instance method called m1() as such method is not defined in the interface.

Similarly, when you program to interfaces with lists like List<String> list = new ArrayList<>(); you don't expect to be able to call list.ensureCapacity(123);.
 
Stephan van Hulst
Bartender
Posts: 15737
368
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To expand on what Piet said, "Programming to the interface" is advice that is only applicable to calling instance methods.

Static methods should only be called by qualifying the call with the declaring type name (e.g. calling B.m1() ).

Sadly, with static methods declared in classes, you can call them through an object reference (b.m1() ). With static methods in interfaces, the Java designers wizened up and made it illegal to call them through object references.
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic