About Question enthuware.ocpjp.ii.v11.2.3510 :

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

Moderator: admin

Post Reply
aPerson
Posts: 17
Joined: Fri Aug 12, 2022 10:19 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3510 :

Post by aPerson »

Hi, a couple of the answers talk about redeclaring default methods. Isn't redeclaring (and hiding the original method) only for static methods? I tested different scenarios with the @Override-annotation and it seems to be in agreement with this.

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

Re: About Question enthuware.ocpjp.ii.v11.2.3510 :

Post by admin »

I did not understand what exactly are you getting at. Can you explain your point with an example?

aPerson
Posts: 17
Joined: Fri Aug 12, 2022 10:19 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3510 :

Post by aPerson »

Well, it's a general question actually; can a default method in an interface be redecleared in an extending interface? I thought it can only be overridden.
The following code is one of the correct answers and it is referred to as a redeclaration. Isn't it an override?

Code: Select all

interface Book {
    public default String getId() {
        return "ISBN123456";
    }
}

interface Encyclopedia extends Book {

    default String getId() {
        return "AIN8888";
    }
}

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

Re: About Question enthuware.ocpjp.ii.v11.2.3510 :

Post by admin »

Well, redeclaration is a non technical term. Technically meaningful terms would be overriding and hiding. Redeclaration just implies having the same method being present in a sub type. Whether that constitutes a valid override or a valid hiding depends on the situation.
From that perspective, I think yes, you can redeclare a default method of an interface in the sub-interface.

AlienFS
Posts: 9
Joined: Tue Sep 05, 2023 4:07 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3510 :

Post by AlienFS »

The question starts with:

Code: Select all

interface Book {
  public default String getId() {
    return "ISBN123456";
  }
}
interface Encyclopedia extends Book {
  //INSERT CODE HERE
}
your "good" answer:

Code: Select all

default String getId(){
  return "AIN8888";
};
An interface can redeclare a default method and provide a different implementation.
The visibility in the original class Book is public. You cannot reduce the visibility in a subclass, right? So your answer is not correct?

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

Re: About Question enthuware.ocpjp.ii.v11.2.3510 :

Post by admin »

What happened when you tried compiling it?

AlienFS
Posts: 9
Joined: Tue Sep 05, 2023 4:07 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3510 :

Post by AlienFS »

Well it compiles. So, obviously, you are in the right. I didn't expect a default method to be allowed to change the visibility.

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

Re: About Question enthuware.ocpjp.ii.v11.2.3510 :

Post by admin »

No, the reason it compiles is because all methods defined in an interface are public by default, unless they are explicitly defined as private. So, the default method in the given problem statement didn't change visibility. It is public even though not defined as public implicitly.

AlienFS
Posts: 9
Joined: Tue Sep 05, 2023 4:07 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3510 :

Post by AlienFS »

Ah yes. I got it mixed up as an interface itself has default visibility "package".

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests