About Question enthuware.ocpjp.i.v11.2.1481 :
Posted: Sun Jun 14, 2020 1:03 pm
Greetings,
I can understand An interface can redeclare a default method and provide a different implementation. because it works like an Override.
But I don't understand the reasoning behind An interface can redeclare a default method and also make it abstract. I can just memorize this fact for the test, but It would be easy to forget if I do not understand the reasoning behind this.
Below are my hypothesis:
1. Because abstract and default keywords can go together -> WRONG. The book also states, "a method cannot be default as well as abstract at the same time".
2. Because all methods without body inside an interface are automatically treated as abstract -> WRONG
3. Because this is not an Override -> WRONG
Adding @Override allows the code to compile fine
4. Because the child method only has the same name, but is a completely new method. -> WRONG
5. An overriding method is allowed to strip the default keyword from the overridden method. -> This kinda makes sense because Java should allow inheriting interfaces/classes/abstract classes to strip the default keyword and make their own implementations.
5b. Unlike accessibility modifiers(private, package, protected, public), the keywords abstract and default do not affect Overridability whatsoever?
Are my reasonings in #5 and #5b correct? If not, then can you explain the concept behind
An interface can redeclare a default method and also make it abstract.
a little bit more?
Thanks.
I can understand An interface can redeclare a default method and provide a different implementation. because it works like an Override.
But I don't understand the reasoning behind An interface can redeclare a default method and also make it abstract. I can just memorize this fact for the test, but It would be easy to forget if I do not understand the reasoning behind this.
Below are my hypothesis:
1. Because abstract and default keywords can go together -> WRONG. The book also states, "a method cannot be default as well as abstract at the same time".
Code: Select all
//abstract default String getId(); //1 Will not compile
//default abstract String getId(); //2 Will not compile
Code: Select all
//public default String getId(); //3 Will not compile because the kw [i]default[/i] is present
Adding @Override allows the code to compile fine
4. Because the child method only has the same name, but is a completely new method. -> WRONG
Code: Select all
void getId(); //Changing the child method return type. This is treated as an Override and requires a covariant return type, so it will not compile.
5b. Unlike accessibility modifiers(private, package, protected, public), the keywords abstract and default do not affect Overridability whatsoever?
Are my reasonings in #5 and #5b correct? If not, then can you explain the concept behind
An interface can redeclare a default method and also make it abstract.
a little bit more?
Thanks.