Full code form task link.
Hello,
Why this didn't work?
public static String getObjectType(Object o) {
//tutaj wpisz swój kod
if (o instanceof Cat) {
return "Cat";
} else if (o instanceof Tiger) {
return "Tiger";
} else if (o instanceof Lion) {
return "Lion";
} else if (o instanceof Bull) {
return "Bull";
} else if (o instanceof Cow) {
return "Cow";
} else return "Animal";
}
Output:
Cat
Cat
Cat
Bull
Cow
Animal
for Tiger and Lion -> error: is always false:
and this too don't works:
if (o instanceof Tiger && o instanceof Cat) {
return "Tiger";
error: is always false:
And this works.
public static String getObjectType(Object o) {
//tutaj wpisz swój kod
if (o instanceof Cat) {
if (o instanceof Tiger) {
return "Tiger";
}
else if (o instanceof Lion) {
return "Lion";
}
return "Cat";
} else if (o instanceof Bull) {
return "Bull";
} else if (o instanceof Cow) {
return "Cow";
} else return "Animal";
}
Output:
Cat
Tiger
Lion
Bull
Cow
Animal
I don't even know what to call what went on here.
A nesting queue? The condition is first checked in the inherited class and then the subclass?