Is it possible to overload methods across classes? I tried out this code, and it works fine: import java.io.*; class Super { int methodOne( int a, long b ) throws IOException { // code that performs some calculations return a; } float methodTwo( char a, int b ) { // code that performs other calculations float f= 0; return f; } } public class Test11 extends Super { public static void main(String args[]) { Test11 t11 = new Test11(); t11.methodTwo(5); } void methodTwo(int f){ System.out.println("Overloaded method: "+f); } } Output: Overloaded method: 5 Does this mean that the compiler takes methodTwo() in class Test11 as an overloaded method? If I change the header of t11.methodTwo to "float methodTwo( char x, int z )" does it mean that the compiler now takes t11.methodTwo as an overriding method? <confused> Thanks in advance
Bosun Bello
Ranch Hand
Joined: Nov 06, 2000
Posts: 1506
posted
0
Yes, it's possible to overload methods accross sub/super class. Your assumptions are correct...a method in a subclass with the same name but different signature from the method in the superclass, will overload the method inthe superclass. If the signature, return type and name matches the method in the superclass, then it will override it.
------------------ Bosun
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Gaia Nathan
Ranch Hand
Joined: Aug 01, 2001
Posts: 62
posted
0
Hi Bosun, Thanks for your reply. I'm clear about it now. If we then wish to use the overloaded super class method in the subclass, do we call it like this: "super.methodTwo('c',7);" (providing that the method in the super class is not declared private) ? If we wish to use the overloaded subclass method in the subclass, would we call it like this: "this.methodTwo(2.3);" ? (regardless of its access modifier) Sorry for being so tedious. Thanks again.
The 'this' keyword is not required in the case where you are calling any method from within the class in which it's defined (whether it overrides a super classes' method or not)
Gaia Nathan
Ranch Hand
Joined: Aug 01, 2001
Posts: 62
posted
0
Ohhh..okay..Thanks again.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.