consider this code:
class Super {
int methodOne( int a, long b ) {
// code that performs some calculations
return a;
}
}
class Sub extends Super {
int methodOne( int b ) {
// code that performs some calculations
return b;
}
}
public class Test11a {
public static void main(
String args[]) {
Sub s13 = new Sub();
System.out.println(s13.methodOne(8));
Super s12 = new Sub();
// System.out.println(s12.methodOne(8)); //Error in Super
// System.out.println( ((Super) s12.methodOne(8) )); //Same Error in Super
// System.out.println( ((Sub) s12.methodOne(8) )); //Same Error in Super
}
}
Result: 8
If I decoment one of the lines calling s12.methodOne(8) then this error is being displayed:
C:\jdk1.3\bin>javac Test11a.java
Test11a.java:21: methodOne(int,long) in Super cannot be applied to (int)
System.out.println(s12.methodOne(8)); //Error in Super
^
1 error
My Question is: When I override methodOne by defining the same method name with same signature and return type in Sub class then the Sub class method methodeOne(int b) would be invoked when Super s12 = new Sub().
When I override methodOne by defining the same method name with different signature (= argument lis) or return type then there�s no
Polymorphism an Superclass methodOne( int a, long b) is called.
Why is that the case that methodeOne(int a, long b) is called?
When always methodeOne(int a, long b) is called: What is the sense of overloading across classes? Wouldn�t it be better to define all overloaded methodes in the Super class instead spreading it across classes?
I appreciate your answers.
Thomas.