aspose file tools*
The moose likes Beginning Java and the fly likes Overloading vs Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloading vs Overriding" Watch "Overloading vs Overriding" New topic
Author

Overloading vs Overriding

Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
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: 1510
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
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.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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
Ohhh..okay..Thanks again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading vs Overriding