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("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??



LVL 20
chaitu chaituAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

petmagdyCommented:
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 3 methods, in order to correct this situation if for example u want to call ( void m1(StringBuffer t) ) then u have to do this:

  a.m1( (StringBuffer) null);



0
chaitu chaituAuthor Commented:
in ur case if i remove below method class compiled fine;
void m1(StringBuffer t)
 {
  System.out.println("StirngBuffer");
 }

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;

0
suprapto45Commented:
Hi,

Well, why "reference to m1 is ambiguous" error raised? It happens because when you run a.m1(null);, the Java compiler will check which m1 method should it call based on its parameter (in this case is null).

As there are two methods that can handle null parameter - void m1(Exception t) and void m1(StringBuffer t), the Java compiler will throw exception as it does not know which method to call. Please remember, Exception can be null and StringBuffer can also be null.

If you want to call the "void m1(StringBuffer t)", you can do the call method as follow.
a.m1(new StringBuffer());

If you want to call the "void m1(Exception t)", you can do the call method as follow.
a.m1(new Exception());

Thanks and I hope that helps.

Regards
Dave
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petmagdyCommented:
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)

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.