Code:
class TestA {
public void foo(String... strings ) {
System.out.println("TestA::foo");
}
public void bar(String a){
System.out.println("TestA::bar");
}
}
class TestB extends TestA {
public void foo(String strings ) {
System.out.println("TestB::foo");
}
public void bar(String a){
System.out.println("TestB::bar");
}
public static void main(String[] args) {
TestA a = new TestB();
a.foo("foo");
a.bar("bar");
}
}
Output is
TestA::foo
TestB::bar
So B::bar is overridden and B::foo is overloaded and when a function is overloaded, it is the data type of the reference that matters not the type of object it is pointing to. Am I right?
Yes.
Overloading is compile time binding and only the type of reference is known at that time. While overriding is run time binding and based on type of object the calls are executed.