Thursday, August 11, 2016

A tip on inheritence

if a class has two method overloaded with parameters with inheritance relationships, when method is invokded with different type of objects, the method with up most parent class will be invoked.



public class A {
 String getV(){
return "A";
 }
}


public class B extends A {
String getV(){
return "B";
}
public static void test(A a){
System.out.println(a.getV());
}
public static void test(B b){
System.out.println(b.getV());
}
public static void main(String[] arg){
A a = new A();
A b = new B();
test(b);
test(a);
}
}

In the testing, only test(A a ) will be invoked.

No comments: