1. Make the Fox class implement the Animal interface.
2. Edit the code so that the getName method is the only method in the Fox class.
3. Don't create additional classes or delete any methods!
Requirements:
1. The Fox class must implement the Animal interface.
2. The Fox class must only implement one method (getName).
3. The Animal interface must declare a getColor method.
4. Don't create additional classes or interfaces.
package com.codegym.task.task13.task1313;
import java.awt.*;
/*
A fox is an animal
*/
public class Solution {
public static void main(String[] args) throws Exception {
}
public interface Animal {
Color getColor();
public String getName();
}
public static class Fox implements Animal{
public String getName() {
return "Fox";
}
}
}
How to solve this question??
Under discussion
Comments (10)
- Popular
- New
- Old
You must be signed in to leave a comment
PeterC Android Developer
24 December 2021, 18:22
I solved it by making the getColor() a default method that returns null.
0
Ivan
2 December 2019, 09:39
You can have 1000 implemented methods, but should you have one abstract the class must be declared abstract.
Intellij helps a lot with underlying :)
But anyway, in real world, down the road, you must implemented with a subsequent child class the abstract method. It maybe 10 classes below, but it must, bear in mind :)
0
Deepak Joshi
9 May 2019, 18:40
make Fox class, abstract
+18
Henk
16 May 2019, 09:19
I thought it's already automatically abstract due to the fact that you extended an Interface ?
0
Deepak Joshi
17 May 2019, 13:13
No, it's not, you have to specifically mention it. Otherwise, you will get this message.
Fox is not abstract and does not override abstract method getColor() in Animal
+3
Guadalupe Gagnon
24 April 2019, 14:05
https://stackoverflow.com/questions/11437097/not-implementing-all-of-the-methods-of-interface-is-it-possible
+5
John
27 May 2021, 10:56
What happens when an abstract class implements an inteface, and why dont it require to override the Color getColor() method, since the lesson says that you must implement all the methods in an interface
+1
Guadalupe Gagnon
27 May 2021, 13:42
interface methods are abstract, so when an abstract class implements an interface the methods can be implemented in the abstract class, but you don't have to implement them there. The very first non-abstract class that extends that abstract class in the class hierarchy MUST implement any abstract methods (abstract being that they were not implemented) from that parent class:
+3
Guadalupe Gagnon
27 May 2021, 14:32
In that code:
1) there is an interface
2) there is an abstract class that implements that interface
- the class is not required to implement any of the methods, though it can implement any of them if you choose to
- I implemented method1() just to demonstrate that
3) Next is a child class that extends the abstract class. Because it is not abstract itself it now MUST implement any abstract methods from the parent (as well as any abstract methods the parent inherits)
4) Next is another child class. Because this one also directly extends the abstract class it is the first class to do so in its "tree branch". It MUST also implement any abstract methods received from its parent
5) Then I have one last class that extends
+3
John
28 May 2021, 02:32
thank you so much guadalupe. god bless you :)
+1