Here are two examples:
public class A {
public void foo(A a) {
System.out.println("in A");
}
}
public class B extends A {
public void foo(B b) { // (1)
System.out.println("in B");
}
public void f(Object o) { // (2)
System.out.println("in B");
}
}
I don’t understand how come (1) and (2) are considered to be an overrided method for A’s foo(). method number 1 accepts a lower class than the original foo(), I mean that I can’t send her any class of A. as I see it, (1) does not extend foo(), but it is still counts as an override- why?( Similar argument for (2), but opposite).
The reason that I made me think that it is overrided is this:
when I’m trying to run
B b = new B();
b.foo(b);
It checks if A has a foo that accepts B. since every B is an A, it does have one so it ought to print “in A” and not “in B” because B does not overrides it. but it prints “in B”..
Neither of them override the super class A’a method.
When I compile the above I get errors:
But note that it is ok to return a subclass from an overriding method. The following compiles without error.