0

Why this code give ambiguous method error?

class Main { public static void main(String[ ] args) { Hello h=new Hello(); h.show(null); } } class Hello { void show(String s) { System.out.println("String method"); } void show(Object[ ] s) { System.out.println("Object method"); } }

30th Dec 2016, 10:59 PM
pavan singh
pavan singh - avatar
22 odpowiedzi
+ 2
In java, there are 2 different types: 1. Primitive (or value) type which holds a specific value 2. Reference type which holds the reference (address) to the value stored in memory. In your example, both string and object array are considered as reference types. Quoted from http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.1 "There is also a special null type, the type of the expression null (§3.10.7, §15.8.1), which has no name. Because the null type has no name, it is impossible to declare a variable of the null type or to cast to the null type. The null reference is the only possible value of an expression of null type. The null reference can always be assigned or cast to any reference type (§5.2, §5.3, §5.5). In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type." Hence, when you try to invoke your show(null) method, the compiler doesnt understand which actual overloaded method you're trying to use because null can be casted to any reference type, thus the ambiguous error. Hope this helps.
30th Dec 2016, 11:56 PM
Alex Soh Chye Wat
Alex Soh Chye Wat - avatar
+ 2
Not sure, but probably because String IS an Object.
31st Dec 2016, 12:26 AM
Heroes Killer
Heroes Killer - avatar
+ 1
@AKC @HeroesKiller In the end , we all have the same understanding but maybe @pavansingh misunderstood null type as regular string literal
31st Dec 2016, 12:17 AM
Alex Soh Chye Wat
Alex Soh Chye Wat - avatar
+ 1
@HeroesKiller string is an object. If you evaluate if (string instanceof object) i believe it will return true.
31st Dec 2016, 12:30 AM
Alex Soh Chye Wat
Alex Soh Chye Wat - avatar
+ 1
@Heroes Killer sorry, i misunderstood your statement earlier. But i think you're right :). @Paven anyhow, we should not try to code something that'll cause ambiguity from the compiler and the reader. :)
31st Dec 2016, 12:41 AM
Alex Soh Chye Wat
Alex Soh Chye Wat - avatar
+ 1
As AKC said, String does not inherit from Object[]. java.util.Object is a parent class. Object[] is a child class, that extends Object class. String is also a child class, that extends Object class, but not Object[] class. Both String and Object[] are children of Object, but neither of them is child of each other. :D
31st Dec 2016, 12:47 AM
Heroes Killer
Heroes Killer - avatar
0
Because String is an object and null can not be identified as String or object so theres a problem with your method parameters in hello
30th Dec 2016, 11:03 PM
Andreas K
Andreas K - avatar
0
but here null is treated as literal.
30th Dec 2016, 11:05 PM
pavan singh
pavan singh - avatar
0
Please post the detailed exception stacktrace
30th Dec 2016, 11:06 PM
Andreas K
Andreas K - avatar
0
You are passing null as a pointer to nowhere, it cannot be considered as an object. If you want to pass it as a string, you need to call h.show("null");
30th Dec 2016, 11:28 PM
Heroes Killer
Heroes Killer - avatar
0
but if we comment any one show method in Hello class then it's run fine.just try and then after give the answer.....
30th Dec 2016, 11:34 PM
pavan singh
pavan singh - avatar
0
@AKC: I know, just tried to explain it a little differently. @ pavan singh: In this case, null can be considered both as a String and Object array. That's why commenting one of methods helps. It's NOT String or Object, but both types can have a reference to nowhere - null.
31st Dec 2016, 12:04 AM
Heroes Killer
Heroes Killer - avatar
0
I think you did not get the point of all three given answers. You have 2 methods show() and the value took to execute is null. In this case the Environment could execute both of them. Because String is an object aswell you can assign them null, too. But in Java the environment always has to know precisly what it has to do. This is the case if every call belongs to a single identificable method header. And this is exactly the problem For the parameter value null you can not identify the method which should be executed
31st Dec 2016, 12:05 AM
Andreas K
Andreas K - avatar
0
sry @Heroeskiller I am a little bit frustrated from being copied so often. Because I can not see the sense in it. But you are right describing it in another way is a good option.
31st Dec 2016, 12:07 AM
Andreas K
Andreas K - avatar
0
@AKC: No problemo, I know that feel as well. :D In the last case, all three of us replied at once with practically same answer. ^^
31st Dec 2016, 12:10 AM
Heroes Killer
Heroes Killer - avatar
0
@heros killer: if we take Object type argument instead of Object [ ] type in method show then what's the o/p....
31st Dec 2016, 12:15 AM
pavan singh
pavan singh - avatar
0
kk frnds why this code not give ambiguous method error..... class Main { public static void main(String[ ] args) { Hello h=new Hello(); h.show(null); } } class Hello { void show(String s) { System.out.println("String method"); } void show(Object s) { System.out.println("Object method"); } }
31st Dec 2016, 12:22 AM
pavan singh
pavan singh - avatar
0
Nevertheless, it seems he still miss the point. :D I said Object array. That thing, which is passed to the method (an argument) is just a reference. Reference of any type. And all you do is setting that reference to nowhere, which you can do with any reference.
31st Dec 2016, 12:23 AM
Heroes Killer
Heroes Killer - avatar
0
@Corsair Alex: I know, it is a class that extends the java.lang.Object class. Just wasn't sure if that's the reason. =)
31st Dec 2016, 12:33 AM
Heroes Killer
Heroes Killer - avatar
0
but Object [ ] ref is also Object type and extends by String class
31st Dec 2016, 12:37 AM
pavan singh
pavan singh - avatar