Your code as written will not successfully compile. You cannot say "A ac = new C();" because C does not extend A. If you claim to have run this and gotten output, you must have mis-copied something from your running code into this post.
If, for the sake of argument, your code really said "A ac = new A();", then your code still wouldn't run, because A.someMethod
takes an A
, not a C
. The statement ac.someMethod(c)
executes the function A.someMethod
. ac
is of type A
, so executing any function against ac
will get the function of that name from class A
. Trying to pass a parameter of type C
to a function declared to take a parameter of type A
will not "switch" you to using a function from a different class that does take such a parameter. Overloading only works within a class.
Perhaps what you're thinking of is an example more like this:
class A {
public void someMethod(A a) {
System.out.println("A");
}
public void someMethod(B b) {
System.out.println("B");
}
}
class Dmd {
public static void main(String[] args) {
A a = new A();
B b = new B();
a.someMethod(b);
}
}
This will output "B".
The difference here is that the class A
has two versions of the function someMethod
. One of them takes an A
, one takes a B
. When we call it with a B
, we get the B
version.
Do you see how this is different from your example? You are declaring three classes each with a function someMethod
. Here we have one class with two functions both named someMethod
. That's very different in Java.