1

Consider a class A that implements an interface B which has a static method info(). Why is not possible to do A.info()? If it was a default method, it would be possible to do A.info() and I don't understand why there is a difference: they are two kinds of methods that are not abstract but are already defined in the interface, the difference should be only the fact one is static and the other is not.

Azeem
  • 11,148
  • 4
  • 27
  • 40
dyso
  • 37
  • 4
  • Static methods belong to the class and are not inheritable. Therefore there is not such thing a `A.info()`, the static method `info()`, from your example, belongs to the class `B` and should be called by doing `B.info()` to avoid any confusion. – Edwin Dalorzo Jul 23 '17 at 04:54
  • Can you please provide example code in addition to the explanation. Read [mcve] for tips on providing a good code example. – Code-Apprentice Jul 23 '17 at 04:56

2 Answers2

0

Keep in mind: there is no polymorphism for static methods.

So, when you have two classes A and B - yes, then you can call A.info(). But that is a discouraged practice - as you are still calling the static method from B.

Thus guessing: the people behind the Java language simply want to prevent that people start misusing static methods in interfaces this way.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

There is a difference because you can implement multiple interfaces. What if you have an interface C which also has a static method info() and you implement both B and C in class A. This would create a problem, that's why it's not allowed.

But when you have two classes A and B, then it's possible because you can only extend one class.

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • But that could happen also with default methods: a class can implement two interfaces with a default method with the same signature. – dyso Jul 23 '17 at 05:14