chaitu chaitu
asked on
what will be the output and why
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("Throwa ble");
}
void m1(Exception t)
{
System.out.println("Except ion");
}
void m1(StringBuffer t)
{
System.out.println("Stirng Buffer");
}
}
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("Stirng Buffer");
}
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??
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("Throwa
}
void m1(Exception t)
{
System.out.println("Except
}
void m1(StringBuffer t)
{
System.out.println("Stirng
}
}
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)
if i remove this method
void m1(StringBuffer t)
{
System.out.println("Stirng
}
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??
ASKER
in ur case if i remove below method class compiled fine;
void m1(StringBuffer t)
{
System.out.println("Stirng Buffer");
}
at that time also
u said "because null will not lead the complier to call which method of the 3 defined in this case as if null is accepted as a parameter for the 2 methods"
how the compiler decided it should go to only to exception argument paameter;
void m1(StringBuffer t)
{
System.out.println("Stirng
}
at that time also
u said "because null will not lead the complier to call which method of the 3 defined in this case as if null is accepted as a parameter for the 2 methods"
how the compiler decided it should go to only to exception argument paameter;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ok when u commented void m1(StringBuffer t) the left 2 methods the parameters Exception and Throwable it happened that (Exception is based on throwable), in this case the Child class has more periority when to chose a target method thats why it decided to call void m1(Exception t)
a.m1( (StringBuffer) null);