• 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

Class and Interface with static method

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this code write my self.

Program : 1


Program : 2


I know the rule of Program :2, but Program : 1 rule not know.
Please suggest me and correct me.
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's just the way it is.
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The java tutorials mention this rule specifically here:

Java Tutorials wrote:Note: Static methods in interfaces are never inherited.

Also have a look at how method hiding works in the same tutorial.
Next, do not write your classes as "Child" and "Parent" since java inheritance is not analogous to a parent-child relationship.
 
Rancher
Posts: 103
4
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By second scenario, you would create ambiguity, because you can call static method of a class through instance of that class, BUT you can NOT call static method of an interface via instance of its implementation, so there is no ambiguity in that case.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Miroslav Lehotsky wrote:By second scenario, you would create ambiguity, because you can call static method of a class through instance of that class, BUT you can NOT call static method of an interface via instance of its implementation, so there is no ambiguity in that case.



I suspect that the reason for this difference is that a class can only extend one class, whereas it can implement more than one interface. So then what if a class implements two interfaces which both have a static method named doStuff()? To allow access to doStuff() via an instance of that class would clearly be ambiguous, and so that's why it isn't permitted.
 
Miroslav Lehotsky
Rancher
Posts: 103
4
VI Editor Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Seems reasonable. So to sum it up, reason for why it isn't permitted to call static method of an interface using instance of implementation of such interface is ambiguity in case of implementing multiple interfaces with own versions of same static method. And this inability to call interface's static method by instance of its implementation is in exchange removing ambiguity in case of providing non-static version of same method in subclass.
 
Ng Sharma
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Best Answer:
https://stackoverflow.com/questions/25169175/why-are-class-static-methods-inherited-but-not-interface-static-methods
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic