Just confused on how to following answer is correct.
class Cat {
public void isClawedBy(Cat c){
System.out.println("Clawed by a cat");
}
}
class Kitten extends Cat{
public void isClawedBy(Kitten c){
System.out.println("Clawed by a Kit");
}
}
If the following is called
Cat g = new Cat();
Cat s = new Kitten();
Kitten t = new Kitten();
g.isClawedBy(t);
s.isClawedBy(t);
t.isClawedBy(t);
How is the answer: Clawed by Cat Clawed by Cat Clawed by Kitten
I'm confused on why s.isClawedBy(t) = Clawed by Cat. Since the dynamic type of s is a kitten, and t is a kitten. Is it because the arguments are different, so it doesn't override it?
Another part I am confused on. //Note the arguments have been swapped.
class Cat {
public void isClawedBy(Kitten c){
System.out.println("Clawed by a cat");
}
}
class Kitten extends Cat{
public void isClawedBy(Cat c){
System.out.println("Clawed by a Kit");
}
}
If the following is called
Cat g = new Cat();
Cat s = new Kitten();
Kitten t = new Kitten();
g.isClawedBy(t);
s.isClawedBy(t);
t.isClawedBy(t);
The output is: Clawed by Cat Clawed by Cat Clawed by Cat
How does it work for when t is called?
About the second query : t.isClawedBy(t)
giving the output of Clawed by Cat
.
Since t
is a Kitten
and the argument passed in the method t.isClawedBy(t)
is also Kitten
, the method from the superclass Cat
will be called because it matches the arguments perfectly.