• 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

what will be the output and why

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package example;

class dup
{
public static void main(String[] args)
{
System.out.println("Hello World!");
dup a=new dup();
a.m1(null);
}
void m1(Throwable t)
{
System.out.println("Throwable");
}
void m1(Exception t)
{
System.out.println("Exception");
}
void m1(StringBuffer t)
{
System.out.println("StirngBuffer");
}

}


when i run this class i am gettiing this exception

Error(9,5): reference to m1 is ambiguous; both method m1(java.lang.Exception) in class example.dup and method m1(java.lang.StringBuffer) in class example.dup match

if i remove this method

void m1(StringBuffer t)
{
System.out.println("StirngBuffer");
}

application is compiled fine;

when i run the class

i am getting o/p

Hello World!
Exception

but i assumed o/p will be

Hello World!
Throwable

why??
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When resolving overloaded methods, the compiler chooses the most specific applicable overload. Because Exception is a subclass of Throwable the m1(Exception) overload is "more specific". On the other hand, there's no way to decide which of Exception or StringBuffer matches better, so with all three methods, you get the compile error.
 
Something about .... going for a swim. With this tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic