• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloading accross classes vs Overriding

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if you don't own the Super class, but you need the Sub class to have the additional signature. For instance, I would hope that you would never tinker with the class files that are provided by Sun, however that does NOT mean that they are all inclusive for all situations. The appropriate thing to do is subclass the Sun class and add in the stuff you need.
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To answer the question Why is that the case that methodeOne(int a, long b) is called?
I think its because when you say Super s12 = new Sub(); you are creating an object of type Sub, but saying that it is going to adhere to the "Super" contract. The is no method with the signature method1(int) in Super.
For example, if I say Object o = new File();, o is going to adhere to the contract set forth by Object. I have an Object reference to an instanceof File. I wont be able to call any File methods on it, only Object methods. I can however, cast it to a File by saying File f = o, because even though o is adhering to the Object contract, it is still an istanceof File.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic