I don't understand why the ab.m3()
method calls the function of the parent class and not the child. I thought maybe passing a new Integer
to the method might call the method of the parent class because Integer
is an Object
so I tried it with an int
but still it gave me the same result!
public class A {
public void m1(){
System.out.println("A.m1");
}
public void m2(){
System.out.println("A.m2");
}
public void m3(Object x){
System.out.println("A.m3");
}
}
public class B extends A{
public void m1(){
System.out.println("B.m1");
}
public void m2(int x){
System.out.println("B.m2");
}
public void m3(int x){
System.out.println("B.m3");
}
public static void main(String[] argv){
A aa = new A();
A ab = new B();
int num = 2;
ab.m1();
ab.m2();
ab.m3(new Integer(2));
ab.m3(num);
}
}
Output:
B.m1
A.m2
A.m3
A.m3