In the following code:
b.show("str");
//prints: from base
d.show("");
//prints: from str
Could one please explain why it's behaving differently? I am wondering why Base b = new Sub(), that b.show() from base class would be invoked. I am merely using DifferentClass, as an reference showing b.show(String) is called under an non-inheritance occasion.
public class TestClass {
public static void main(String[] args) {
Base b = new Sub();
b.show("str");
DifferentClass d = new DifferentClass ();
d.show("");
}
}
class Base {
public void show(Object obj) {
System.out.println("from base");
}
}
class Sub extends Base {
public void show(String str) {
System.out.println("from sub");
}
}
class DifferentClass {
public void show(String str) {
System.out.println("from str");
}
public void show(Object obj) {
System.out.println("from obj");
}
}