0

Reading article about java 8 static and default interface methods, and i found

Java interface static method is visible to interface methods only,

so my guess is that static interface methods will not inherited by the concrete classes

at the same articel

We can’t define interface static method for Object class methods, we will get compiler error as “This static method cannot hide the instance method from Object”. This is because it’s not allowed in java, since Object is the base class for all the classes and we can’t have one class level static method and another instance method with same signature.

if interfaces are not inherited from Object class and static interface methods will not be inherited by the concrete class, Why we can’t define interface static method for Object class methods

EDIT

why we can't add public static int hashCode(){//SOME_CODE;} into interface?

Melad Basilius
  • 3,847
  • 10
  • 44
  • 81
  • 2
    Then you wouldn't ever be able to inhert from said `interface`. What would such a `interface` be for? – Boris the Spider Jul 22 '18 at 18:05
  • @BoristheSpider, your comment is not clear, can you add more explanation please. Thanx – Melad Basilius Jul 22 '18 at 18:09
  • @MeladEzzat What exactly is the question/problem you have? What do you want to do? – Progman Jul 22 '18 at 18:10
  • @Progman, why we can't add public static int hashCode(){} into interface – Melad Basilius Jul 22 '18 at 18:13
  • 5
    We can't do `public static int hashCode() {...}` in a regular class either. – Radiodef Jul 22 '18 at 18:14
  • @Radiodef, I know and i understand why we cannot do it in regular classes. the point is why we cannot do it in interfaces. – Melad Basilius Jul 22 '18 at 18:16
  • 2
    What would be the point of `hashCode` being `static` (even in interface)? Anyway related post: [Java8: Why is it forbidden to define a default method for a method from java.lang.Object](https://stackoverflow.com/q/24016962) – Pshemo Jul 22 '18 at 18:17
  • 1
    "Java interface static method is visible to interface methods only," is not true. There are many static or default methods which are available outside of interface like in case of `interface Comparator` we can use `reverseOrder()` which is static method available to "outside world" or `thenComparing(Function)` which is default method. – Pshemo Jul 22 '18 at 18:23
  • @Pshemo, by 'visible' it means visible like any regular inherited method, not through the interface name. – Melad Basilius Jul 22 '18 at 18:25
  • 2
    Visibility describes *scope* from which we can use member of type, not *how* we are accessing it. – Pshemo Jul 22 '18 at 18:26
  • @Pshemo, the bottom line is 'interface static methods are not inherited by the implementing classes'. – Melad Basilius Jul 22 '18 at 18:28
  • @MeladEzzat I don't know why this question is marked as duplicate. I too am confused after reading your question and would like to know a better reason. – Shivam Pokhriyal Jul 22 '18 at 19:52
  • @ShivamPokhriyal, I don't know why Progman and GhostCat marked the question as duplicate. it is completely different question – Melad Basilius Jul 22 '18 at 20:07
  • @Progman it is not the same question – Melad Basilius Jul 22 '18 at 20:08
  • @GhostCat, it is not the same question – Melad Basilius Jul 22 '18 at 20:08
  • All interfaces are always subtypes of class java.lang.Object, so trying to declare a method with signature int hashCode() would be trying to override Object's int hashCode(), but overriding is impossible for a static method. The reasons are not different for a class or for an interface. – kumesana Jul 23 '18 at 00:11
  • @Progman This is not a duplicate. You should reopen this question or comment a better answer here – Shivam Pokhriyal Jul 23 '18 at 12:16

0 Answers0