So, I have this problem (which I am guessing has something to do with dynamic binding) that I can not wrap my head around.
class A {
public int m(A p) {
return 12;
}
public int n(A p) {
return 38;
}
}
class B extends A {
public int m(B p) {
return 3;
}
public int n(A p) {
return 7;
}
}
class C extends A {
public int m(A p) {
return 38;
}
public int n(A p) {
return 4;
}
}
class D extends A {
public int m(A p) {
return 1;
}
public int n(D p) {
return 2;
}
}
class Main {
public static void main(String argv[]) {
A x = new C();
A y = new D();
D z = new D();
System.out.println(x.m(z) + y.n(z)); //*
}
}
The commented line is where I get stuck. Why the result is 76? what does it depend on, because I have been searching all over the internet and I wasn't able to find an example for it. if you can come up with little explanations I would very much appreciate.
It returns 76, because both parts of sum returned 38:
x.m(z)this part means, try to find methodmwithin classA(becausexisA) which can acceptD, there is no such method inA, but there ismwhich acceptsA, as soon asDextends fromA- this method is used, so we're calling methodA.m(A), but this method is overridden inC, so overridden method is actually executed, ieC.m(A)y.n(z)this part follows the same logic and tries to callA.n(A), but classDdoes not override methodn-public int n(D p)is not override forA.n(A), so non overridden method fromAis executedI suggest you to always use
@Overrideannotation to allow compiler to validate what methods are actually overridden and what not, here is annotated version which is easier to follow:to allow compiler to find
n(D)method you must cast y toD, like((D)y).n(z)or use derived type in declarationD y = new D();