Page 1 of 1
About Question enthuware.ocpjp.v8.2.1837 :
Posted: Mon Feb 17, 2020 5:31 am
by bvrulez
For the people who don't understand (like me) without a proper explanation (that is omitted by EthuWare):
The method of Office is static. It can only be referenced in a Static way: Office.getAdress().
Also, both interfaces can be extended by WHF because static is not the same as default.
Re: About Question enthuware.ocpjp.v8.2.1837 :
Posted: Mon Feb 17, 2020 8:28 am
by admin
The following detailed explanation is already provided:
Since the declared type of variable off is Office, compiler will check the call to getAddress against Office interface. However, getAddress in Office is static and Java 8 requires static interface method to be invoked using the interface name instead of a reference variable. That is why, the compiler will raise the following error message:
TestClass.java:26: error: illegal static interface method call System.out.println(h.getAddress()); //2
^ the receiver expression should be replaced with the type qualifier 'Office' 1 error
Please let us know if you are not seeing this explanation.
- test.png (26.38 KiB) Viewed 2711 times
Re: About Question enthuware.ocpjp.v8.2.1837 :
Posted: Mon Feb 24, 2020 7:57 am
by bvrulez
The explanation was not visible to me because I used the wrong layout for Mac. Also, I am still getting this question wrong after one week now...
Re: About Question enthuware.ocpjp.v8.2.1837 :
Posted: Sun Sep 26, 2021 10:30 am
by samba2
I think it is also worth pointing out that static *interface* methods are *not* redeclared + hidden as it would be the case with classes:
https://stackoverflow.com/questions/251 ... ic-methods
So Office.getAddress() does not conflict with the default method of the same name in the House interface.
So a static method in an interface always stays there and does never appear somewhere else. I wasn't aware of that.
Re: About Question enthuware.ocpjp.v8.2.1837 :
Posted: Wed Jan 19, 2022 4:34 pm
by sijucm
@Samba2, that's what I was also looking at. Look at the code below. In the Interface Sub, the static method creates a problem and probably it is because the default method is inherited although the static one is not. But I wonder what is the point of remembering all these weird combinations people should not use in real life.
interface Super {
static void meth(){ }
}
interface Super2 {
default void meth(){ } // this is fine because meth() from Super is not inherited
}
interface Sub extends Super, Super2 {
static void meth(){} // this will not compile because meth() from Super2 is inherited
}