Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

what will be the output and why

Posted on 2004-11-20
4
Medium Priority
?
231 Views
Last Modified: 2010-03-31
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??



0
Comment
Question by:chaitu chaitu
  • 2
4 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12637129
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
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 12637330
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
 
LVL 16

Accepted Solution

by:
suprapto45 earned 500 total points
ID: 12637371
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12637384
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question