After completing the first two requirements, I just started trying different things. I started going through the description rather than the requirements and added
food.onSelect();
food.onEat();
in the foodMethods method to see if the third requirement would pass, and the entire exercise passed. What confuses me even more is that there's nothing in the selectableMethods method, even though the exercise told me to put something there.
My code:
public class Solution {
public static void main(String[] args) {
Food food = new Food(); // food object is created
Selectable selectable = new Food(); // selectable object is created with the same constructor as the food object
Food newFood = (Food) selectable; // newFood object is created with selectable as a constructor
foodMethods(food); // The foodMethods method is called with food as an argument
selectableMethods(selectable); // The selectableMethods method is called with selectable as an argument
}
public static void foodMethods(Food food) {
//write your code here
// The onEat method must be called from the foodMethods method.
food.onSelect();
food.onEat();
}
public static void selectableMethods(Selectable selectable) {
//write your code here
}
public static void onEat(Food food){
}
interface Selectable {
void onSelect();
}
static class Food implements Selectable {
public void onEat() {
System.out.println("The food was eaten");
}
@Override
public void onSelect() {
System.out.println("The food was selected");
}
}
}
It passed the verification, but I don't understand how.
Under discussion
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Gellert Varga
11 June 2021, 23:29
I recommend read this:
https://codegym.cc/help/15388
0
Guadalupe Gagnon
11 June 2021, 19:43
Strange. Requirement 4 should not have passed with this code, but I would just count it as a freebie. The method selectableMethods() takes in a Selectable parameter so it can only access methods that are defined in the Selectable interface (which is the .onSelect()) method.
One thing, your notes say:
However it is not using a Selectable constructor. This is called type casting, where you cast a variable of one type to a variable of another type (providing that the object is an acceptable class of the type you are casting to). So you are taking a variable which was defined as a Selectable, and then on that line casting it to a variable of Food. Take this bit of code for an example:
0
Guadalupe Gagnon
11 June 2021, 19:45
If you uncomment out the last time the IDE will not show an error because a cast Exception is a type of unchecked, which means that it can cause an error during the run time but not during the compile. If you run the program after uncommenting that line an Exception will be thrown for the illegal ClassCastException.
0
Gellert Varga
11 June 2021, 23:37
I tried this program and looked at the output, and I wonder why the overridden toString() method works on obj1. The variable obj1 is of type TestInterface, and this interface has no toString() method...
So I think that only the increase() method should work on the obj1 variable. But JVM didn't think so:)
0
Guadalupe Gagnon
12 June 2021, 19:39
All objects have toString(), its inherited by the base class Object.
0