• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Overriding var-args

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

I have a question about the following code:

class base {
void foo(String s1, String... sA) {
System.out.println("Ti");
}
}

class badSyntax4 extends base
{
public badSyntax4(){}

void foo(String s1, String sA){
System.out.println("hi");
}
public static void main(String[] args){
base b = new badSyntax4();
b.foo("test","test");
}
}

Source : Modified one of the code on bug.sun.com
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4994569

Output: Ti


If I consider it overriding then
1) How the var-args method got overridden by single argument method?
2) Why did the parent class method get called instead of the runtime child class method?

If I consider that the var-args can be overriden by var-args or array only then this as a overload and in that case the output is perfectly okay.

Just want to confirm...
Thanks,
Megha
[ May 13, 2007: Message edited by: megha joshi ]
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1) How the var-args method got overridden by single argument method?
2) Why did the parent class method get called instead of the runtime child class method?



It is an overload and not an override. Try adding another method in the derived class taking an array as the second argument and see that getting called.
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for confirming that the code is a example of overload.I already know about behaviour with arrays.

Thanks,
Megha
 
My pie came with a little toothpic holding up this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic