About Question enthuware.ocpjp.ii.v11.2.3510 :
Moderator: admin
-
- Posts: 17
- Joined: Fri Aug 12, 2022 10:19 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3510 :
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.
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3510 :
I did not understand what exactly are you getting at. Can you explain your point with an example?
-
- Posts: 17
- Joined: Fri Aug 12, 2022 10:19 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3510 :
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?
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";
}
}
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3510 :
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.
From that perspective, I think yes, you can redeclare a default method of an interface in the sub-interface.
-
- Posts: 9
- Joined: Tue Sep 05, 2023 4:07 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3510 :
The question starts with:
your "good" answer:
The visibility in the original class Book is public. You cannot reduce the visibility in a subclass, right? So your answer is not correct?
Code: Select all
interface Book {
public default String getId() {
return "ISBN123456";
}
}
interface Encyclopedia extends Book {
//INSERT CODE HERE
}
Code: Select all
default String getId(){
return "AIN8888";
};
An interface can redeclare a default method and provide a different implementation.
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3510 :
What happened when you tried compiling it?
-
- Posts: 9
- Joined: Tue Sep 05, 2023 4:07 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3510 :
Well it compiles. So, obviously, you are in the right. I didn't expect a default method to be allowed to change the visibility.
-
- Site Admin
- Posts: 10384
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3510 :
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.
-
- Posts: 9
- Joined: Tue Sep 05, 2023 4:07 am
- Contact:
Re: About Question enthuware.ocpjp.ii.v11.2.3510 :
Ah yes. I got it mixed up as an interface itself has default visibility "package".
Who is online
Users browsing this forum: No registered users and 10 guests