• 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

Doubt on Varargs and overriding

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am reading the SCJP Tiger Study Guide, I don't quite follow how the overriding with varargs works on the example given. Please help me out.

public 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);
}
}

import static java.lang.System.out;

public class OneSubclass extends OneSuperclass {

public int doIt(String str, Integer[] data) // Overridden (a)
//public int doIt(String str, Integer... data) // Overridden (b)

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);
}
}

It says that with overriden(a), the output will be the following:
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[])

But with overriden (b), the output is:
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[])

My doubt is:
For output statment 1, 2, 3, 5, 7, When overriden (a) is used, why the OneSuperclass.doIt(String str, Number... data) operation is chosen? Why not OneSubclass.doIt(String str, Integer... data)? Isn't the signature of OneSubclass.doIt(String str, Integer... data) is more matching with the input parameter?

Thanks!


Lyn
[ October 02, 2005: Message edited by: Lyn Yang ]
 
Lyn Yang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no answer?

Somebody please help...
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lyn Yang:
Hi,

But with overriden (b), the output is:


My doubt is:
For output statment 1, 2, 3, 5, 7, When overriden (a) is used, why the OneSuperclass.doIt(String str, Number... data) operation is chosen? Why not OneSuperclass.doIt(String str, Integer... data)? Isn't the signature of OneSuperclass.doIt(String str, Integer... data) is more matching with the input parameter?

Thanks!


Lyn



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[])


See, the compiler runs this way
1. sees if there's a method that matches the sign exactly without any modification
2. it sees if something can be matched after boxing/unboxing all without varargs
3. it sees if something can be matched after boxing/unboxing and varargs

when you box a long, you get a Long which is a subclass of Number and not of Integer, the same is the case with short > Short extends Number

thats why, hope its clear now.
 
Lyn Yang
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Akshay Kiran. However, what I don't understand is when the overriden (a) operation is used in the subclass i.e.
public int doIt(String str, Integer[] data)

However, I think I know the answer when I tried to reorganize the question. I got myself confused as I messed up the overidden (a) and overriden(b)

When the overriden (a) is used, the OneSubclass.doIt(String str, Integer[] data) is not a vararg, so it is expecting an array. Therefore, the 1, 2, 3, 5, 7 will call the OneSuperclass.doIt(String str, Number... data).

B.T.W. Your summary of how compiler works is very clear and precise. Thank you.
reply
    Bookmark Topic Watch Topic
  • New Topic