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.
About Question enthuware.ocpjp.v8.2.1837 :
Moderator: admin
-
- Posts: 33
- Joined: Sat Feb 15, 2020 12:44 am
- Contact:
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1837 :
The following detailed explanation is already provided:
Please let us know if you are not seeing this explanation.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
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Sat Feb 15, 2020 12:44 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1837 :
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...
-
- Posts: 6
- Joined: Mon Sep 06, 2021 1:13 am
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1837 :
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.
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.
-
- Posts: 8
- Joined: Tue Jan 11, 2022 4:41 am
Re: About Question enthuware.ocpjp.v8.2.1837 :
@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
}
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
}
Who is online
Users browsing this forum: No registered users and 84 guests