That's true.... As both the child(DOg,Cat) are at same level so JVM get's confused, which is correct one.
On Fri, Feb 17, 2012 at 11:40 PM, Vishnu Dubey <[email protected]> wrote: > The null when passed as argument will try to fit the appropriate child > class method in case it will find the null argument for Animal, this is > what if i Uncomment the child(Dig D) Method they will fight for > allocation/best fit > >javac Animal.java > Animal.java:13: error: reference to child is ambiguous, both method > child(Dog) i > n Animal and method child(Cat) in Animal match > an.child(null); > ^ > 1 error > > On Fri, Feb 17, 2012 at 10:41 PM, ABHISHEK TRIPATHI < > [email protected]> wrote: > >> Hi All, >> I was keep exploring the solution for this problem and later I found that >> is due to charecteristic of Overloading-Widening/Boxing. >> >> Here is rule: >> "In every case, when Exact match is not found, the JVM uses the method >> with the SMALLEST argument which is Wider then the Parameter. >> So when we pass Null as parameter, JVM will look for the method which has >> next higher level of Argument and it find that CAT is Child of Animal and >> smaller then Animal so it calls method Add from Cat class. >> >> Thanks, >> Abhishek >> On Thu, Feb 16, 2012 at 11:33 PM, Asher Fawad <[email protected]>wrote: >> >>> To tell you the truth, your code is very complex. There is no clarity >>> in there. If you want to learn something, learn it simply. >>> >>> You should define Animal as a separate class with proper method >>> overloading (same method with different arguments). Put main method into a >>> class of its own. >>> >>> I also don't see the purpose of sending arguments like "Cat c" or "Dog >>> d" when you aren't even using it. >>> >>> What do you expect an.child(null) to print??? Of course, it might only >>> print "Animal" (I think it shouldn't print anything). >>> >>> Your method declaration--> void child(Animal a){System.out.println(" >>> Animal");} >>> should be rewritten as [ void child(){System.out.println("Animal");} >>> >>> and your function call an.child(null) should be [an.child()] >>> >>> Advice: >>> >>> Rewrite your program with more clarity and simplicity and then tell if >>> you face same problem. >>> >>> --- On *Wed, 2/15/12, ABHISHEK TRIPATHI <[email protected]>* wrote: >>> >>> >>> From: ABHISHEK TRIPATHI <[email protected]> >>> Subject: [jpassion_java] Overloaded method >>> To: "JPassion.com: Java Programming" <[email protected]> >>> Date: Wednesday, February 15, 2012, 11:04 PM >>> >>> Hi, >>> Can you plz tell why output is coming as Cat not Animal? >>> Also if I un-comment all lines, I get compile time error. >>> >>> thanks >>> Abhi >>> >>> *********************************** >>> package Overload; >>> public class Animal { >>> >>> // void child(Dog d) {System.out.println(" Dog");} >>> void child(Cat c){System.out.println(" Cat");} >>> void child(Animal a){System.out.println(" Animal");} >>> // void child(Mouse m){System.out.println(" Mouse");} >>> // private void child(Cat x){} >>> >>> public static void main(String[] args) { >>> Animal an=new Animal(); >>> an.child(null); >>> } >>> } >>> ************ >>> class Dog extends Animal{} >>> class Cat extends Animal{} >>> class Mouse extends Dog{} >>> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "JPassion.com: Java Programming" group. >>> To post to this group, send email to [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/jpassion_java?hl=en. >>> >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "JPassion.com: Java Programming" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> For more options, visit this group at >> http://groups.google.com/group/jpassion_java?hl=en. >> > > > > -- > Vishnu Narayan Dubey > -- You received this message because you are subscribed to the Google Groups "JPassion.com: Java Programming" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/jpassion_java?hl=en.
