• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Overloading vs Overriding

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Gaia Nathan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhh..okay..Thanks again.
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic