package pl.codegym.task.task13.task1313;
import java.awt.*;
/*
Lis to zwierzę
*/
public class Solution {
public static void main(String[] args) throws Exception {
}
public interface Animal {
Color getColor(); //why we don't need to make sth like: default Color getColor(): ?
}
public static abstract class Fox implements Animal {
public String getName() {
return "Fox";
}
}
}
I thought that I need to declare getColor() method as default but the code works even without it, why?
Rozwiązane
Komentarze (3)
- Popularne
- Najnowsze
- Najstarsze
Musisz się zalogować, aby dodać komentarz
Gellert Varga
31 marca 2021, 20:43rozwiązanie
What does "code works" mean in your question exactly? = Why is JVM able to compile it?
Color getColor(); this is an abstract method without implementation.
Class Fox inherits this, but doesn't make any implementation for this - but since the class itself is abstract also, so there is no compiling-problem.
Class Fox has an inherited abstract getColor() method.
Color getColor(); this method should return a Color object. See the Package java.awt and its Color class.
+2
Igor Karlik
8 kwietnia 2021, 17:15
Thanks a lot, after couple more lesson I realized that answer to this question is quite trivial, only interface methods require implementation :)
0
Gellert Varga
8 kwietnia 2021, 20:46
:-)
"interface methods require implementation" - but only if the implementer class is not abstract.
If the class is abstract, then it's not obligatory to implement the method inherited from an interface.
+1