About Question enthuware.ocpjp.v8.2.1837 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

About Question enthuware.ocpjp.v8.2.1837 :

Post 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.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v8.2.1837 :

Post 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
test.png (26.38 KiB) Viewed 2535 times
If you like our products and services, please help us by posting your review here.

bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1837 :

Post 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...

samba2
Posts: 6
Joined: Mon Sep 06, 2021 1:13 am
Contact:

Re: About Question enthuware.ocpjp.v8.2.1837 :

Post 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.

sijucm
Posts: 8
Joined: Tue Jan 11, 2022 4:41 am

Re: About Question enthuware.ocpjp.v8.2.1837 :

Post 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
}

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 33 guests