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

A doubt in overloading and overriding

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
to all Ranchers here, i have a doubt in overloading and overriding method resolution , while going through questions i came across on this and the question goes like this ...

First question(Overloading) :

public class VarargsOverloading {
public void operation(String str) {
String signature = "(String)";
out.println(str + " => " + signature);
}
public void operation(String str, int m) {
String signature = "(String, int)";
out.println(str + " => " + signature);
}
public void operation(String str, int m, int n) {
String signature = "(String, int, int)";
out.println(str + " => " + signature);
}
public void operation(String str, Integer... data) {
String signature = "(String, Integer[])";
out.println(str + " => " + signature);
}
public void operation(String str, Number... data) {
String signature = "(String, Number[])";
out.println(str + " => " + signature);
}
public void operation(String str, Object... data) {
String signature = "(String, Object[])";
out.println(str + " => " + signature);
}
public static void main(String[] args) {
VarargsOverloading ref = new VarargsOverloading();
ref.operation("1. (String)");
ref.operation("2. (String, int)", 10);
ref.operation("3. (String, Integer)", new Integer(10));
ref.operation("4. (String, int, byte)", 10, (byte) 20);
ref.operation("5. (String, int, int)", 10, 20);
ref.operation("6. (String, int, long)", 10, 20L);
ref.operation("7. (String, int, int, int)", 10, 20, 30);
ref.operation("8. (String, int, double)", 10, 20.0);
ref.operation("9. (String, int, String)", 10, "what?");
ref.operation("10.(String, boolean)", false);
}
}

Output of First Question :
1. (String) => (String)
2. (String, int) => (String, int)
3. (String, Integer) => (String, int)
4. (String, int, byte) => (String, int, int)
5. (String, int, int) => (String, int, int)
6. (String, int, long) => (String, Number[])
7. (String, int, int, int) => (String, Integer[])
8. (String, int, double) => (String, Number[])
9. (String, int, String) => (String, Object[])
10.(String, boolean) => (String, Object[])


Second Question(Overridding) :

import static java.lang.System.out;
class OneSuperclass {
public int doIt(String str, Integer... data) throws java.io.EOFException,
java.io.FileNotFoundException { // (1)
String signature = "(String, Integer[])";
out.println(str + " => " + signature);
return 1;
}
public void doIt(String str, Number... data) { // (2)
String signature = "(String, Number[])";
out.println(str + " => " + signature);
}
}

public class OneSubclass extends OneSuperclass {
//public int doIt(String str, Integer[] data) // Overridden a(1)
public int doIt(String str, Integer... data) // Overridden b(1)
throws java.io.FileNotFoundException {
String signature = "(String, Integer[])";
out.println("Overridden: " + str + " => " + signature);
return 0;
}
public void doIt(String str, Object... data) { // Overloading
String signature = "(String, Object[])";
out.println(str + " => " + signature);
}
public static void main(String[] args) throws Exception {
OneSubclass ref = new OneSubclass();
ref.doIt("1. (String)");
ref.doIt("2. (String, int)", 10);
ref.doIt("3. (String, Integer)", new Integer(10));
ref.doIt("4. (String, int, byte)", 10, (byte) 20);
ref.doIt("5. (String, int, int)", 10, 20);
ref.doIt("6. (String, int, long)", 10, 20L);
ref.doIt("7. (String, int, int, int)", 10, 20, 30);
ref.doIt("8. (String, int, double)", 10, 20.0);
ref.doIt("9. (String, int, String)", 10, "what?");
ref.doIt("10.(String, boolean)", false);
}

Output of Second Question : commenting Overridden a(1)

Overridden: 1. (String) => (String, Integer[])
Overridden: 2. (String, int) => (String, Integer[])
Overridden: 3. (String, Integer) => (String, Integer[])
4. (String, int, byte) => (String, Number[])
Overridden: 5. (String, int, int) => (String, Integer[])
6. (String, int, long) => (String, Number[])
Overridden: 7. (String, int, int, int) => (String, Integer[])
8. (String, int, double) => (String, Number[])
9. (String, int, String) => (String, Object[])
10.(String, boolean) => (String, Object[])

Output of second question : commenting // Overridden (b)

1. (String) => (String, Number[])
2. (String, int) => (String, Number[])
3. (String, Integer) => (String, Number[])
4. (String, int, byte) => (String, Number[])
5. (String, int, int) => (String, Number[])
6. (String, int, long) => (String, Number[])
7. (String, int, int, int) => (String, Number[])
8. (String, int, double) => (String, Number[])
9. (String, int, String) => (String, Object[])
10.(String, boolean) => (String, Object[])


My doubt is here is in method overloading option4 that is
(String, int, byte) => (String, int, int) is resolved as byte is converted to more specific int(rather tahn anyother like float and double ) and mthod is resolved at complie time .
But in case of overriding same option4 that is(commenting Overridden a(1))
(String, int, byte) => (String, Number[]) is resolved , here isn't byte is converted to int (which is more specific than float or double) if so then why it's not calling (String, int, byte) => (String, Integer[]) instead.
and in all options that is commenting // Overridden (b) why methods with integer and bytes as are not calling (String, Integer[]) rather than calling (String,Number[]) form of the method.

Am too confused with this stuff from last 2 days , before asking any favour am feeling sorry to put so long coding , can anyone please explain me with clear details and if possible can give good links on this .
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this used to be my source of headache too.

...these are the issues...
1. widening (ie. byte -> int) or (Float -> Number)
2. boxing (ie. int -> Integer)

...these are the rules...
1. you can box then widen (ie. byte -> Byte -> Number)
2. you cannot widen then box (ie. byte -> int -> Integer)

...so we get...
String -> String
int -> Integer -> Number // box + widen
byte -> Byte -> Number // box + widen
...............................

...but not...
String -> String
int -> Integer // box
byte -> int -> Integer // widen + box -> we can't do this
...............................

have a relaxing time
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bod
i got my answer for first answer of second question , but why second answer of second question is being like that , in that case why all int ,bye ,Integer are casted to Number ,let me mention my doubt clearly.

(All are for second output of second question ).
in subclass there is 3 method which are doIt(String[] str,Integer[] i)derived means overridden in subclass from superclass ,doIt(String[] str,Number[] num) defined in subclass and doIt(String[] str,Object[] obj) defined in subclass.

Option 1 :
we are passing String to doIt mthod then to my knowledge it should go to most specific method which should be (String,Integer[]) version of the method , but it is calling (String,Number[]) version of the method.

Option 2 :
we are passing String and int to doIt mthod then to my knowledge it should go to most specific method which should be (String,Integer[]) version of the method , but it is calling (String,Number[]) version of the method.

Option 3 :
we are passing String and Integer to doIt mthod then to my knowledge it should also go to most specific method which should be (String,Integer[]) version of the method as there is one like that in subclass, but it is calling (String,Number[]) version of the method.

Option 5 :
Here also same thing happening instead of calling (String,Integer[]) version it is calling (String[],Number[]) version.

Option 7:
Same thing happening.

Option 4 :
Option 6 :
Option 8 :
Option 9 :
Option 10:
OK .
which you have clearly described ,I got my answer from your last description.
 
Bod Toki
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are right.

when you compile and run the code for the second answer, all the lines of code you expected to go to (String, Integer[]) actually go to (String, Integer[]) and not (String, Number[]) as the answer advertises.

...there must have been a problem with the material you are studying, try compiling and running the code yourself and you would see your predictions were right.

have a great time studying for your SCJP.
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Bod
I have already tried this program in eclipse and what it is coming is reeally a surprise to me , same answers are coming ,my guess are all wrong , i don't know what is happening behind the screen how it is resolving method calls .If you find any free time kindly run this program and suggest where i am wrong in predicting answers .

Sorry to keep you busy in my question.
 
Bod Toki
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i run a windows xp machine with jdk 1.5 and here are my results for the second answer:

...after compiling the file...


...after running the OneSubclass...


i wonder why you should be getting (String Number[])
..maybe you should try to compile and run the code on someone else's machine to be certain of what happens - and let me know if i'm wrong.
 
Ajit Amitav Das
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Bod , i forgot to tell you that i am doing it in eclipse , here i am also getting eqivalent warning but after running i am geting same output which i had mentioned in previous reply.I think it's happening because of eclipse , i must compile and run it in command prompt . Anyway thanks for your reply.
Have a great day.
 
I child proofed my house but they still get in. Distract them with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic