| Author |
Doubt in overloading
|
aleyx chow
Greenhorn
Joined: May 15, 2005
Posts: 16
|
|
which method would be invoke if the code at line 5 is method.invoke(null,null); if i disable the method 4, method.invoke(null,null)would invoke which method or occur a compile err? i found a way to get the answer but im not sure it will always do the right things. For example, first list out the inhirency chain and mark them a value like following: animal 2 |___cat 1 null 0 second caculate them with the methods param list. result method.invoke(null,null); / 0 / 0 1.void invoke(animal a,animal b){} / 2 / 2 / (2-0)+(2-0) 2.void invoke(animal a,cat b){} / 2 / 1 / 3 3.void invoke(cat a,animal b){} / 1 / 2 / 3 4.void invoke(cat a,cat b){} / 1 / 1 / 2 the No.4 result is min, so the code method.invoke(null,null)will invoke the method invoke(cat, cat) if i disable the method 4,then there will be two same result 3 and 3,so it occur compile err becauseof ambigious method param list. Is it a right way? and some one can show me the rules for invoking a method by the param list?thanx. [ June 07, 2005: Message edited by: aleyx chow ]
|
Oh,father,though im in the nutshell,i still the king of limitless space.<br /> <br />scjp 1.4,scwcd 1.4,scbcd 1.3
|
 |
Nik Raut
Greenhorn
Joined: May 23, 2005
Posts: 22
|
|
I guess your approach is right but someone should give a more specific explanation. Anyone???
|
To be yourself is the best thing to be.
|
 |
ankur rathi
Ranch Hand
Joined: Oct 11, 2004
Posts: 3829
|
|
The method which is more specific, always get called but if compiler feel problem in deciding specific one then gives 'ambigous method' error. Hope it helps.
|
 |
amit taneja
Ranch Hand
Joined: Mar 14, 2003
Posts: 806
|
|
class animal{} class cat extends animal{} class methods{ void invoke(animal a,animal b){System.out.println(" A A");} //method 1 void invoke(animal a,cat b){System.out.println(" A C");} //method 2 void invoke(cat a,animal b){System.out.println(" C A");} //method 3 void invoke(cat a,cat b){System.out.println(" C C");} //method 4 } public class Test14{ methods method=new methods(); //insert code here Line 5 method.invoke (new animal, new animal); } why this error is comming...?? D:\java_prac>javac Test14.java Test14.java:12: <identifier> expected method.invoke (new animal, new animal); ^ 1 error
|
Thanks and Regards,<br />Amit Taneja
|
 |
Tony Thompson
Greenhorn
Joined: Jun 06, 2005
Posts: 14
|
|
Amit, I could be missing a larger issue here but you should have parentheses on your 'animal' constructors as in: method.invoke(new animal(), new Animal()); HTH, Tony
|
 |
Nikhil Pancholi
Greenhorn
Joined: Dec 10, 2004
Posts: 7
|
|
Originally posted by amit taneja: public class Test14{ methods method=new methods(); //insert code here Line 5 method.invoke (new animal, new animal); } why this error is comming...?? 1 error How can one invoke a method from a class? Shouldn't it be something like public class Test14{ methods method = new methods(); public static void main( String[] args) { Test14 test = new Test14(); test.method.invoke(new animal(), new animal()); } } ?
|
 |
amit taneja
Ranch Hand
Joined: Mar 14, 2003
Posts: 806
|
|
Originally posted by Nikhil Pancholi:
Originally posted by amit taneja: [qb] public class Test14{ methods method=new methods(); //insert code here Line 5 method.invoke (new animal, new animal); } why this error is comming...?? 1 error How can one invoke a method from a class? Shouldn't it be something like public class Test14{ methods method = new methods(); public static void main(String[] args) { Test14 test = new Test14(); test.method.invoke(new animal(), new animal()); } } ?[/QB]
ya ur right..i don't know how i ignore such silly thing..
|
 |
vjy chin
Ranch Hand
Joined: Feb 17, 2005
Posts: 279
|
|
I couldnt understand the reasoning, I apologize. It would be great if anyone can explain in layman terms. Thanks
|
 |
Vishnu Prakash
Ranch Hand
Joined: Nov 15, 2004
Posts: 1026
|
|
How can one invoke a method from a class? Shouldn't it be something like public class Test14{ methods method = new methods(); public static void main(String[] args) { Test14 test = new Test14(); test.method.invoke(new animal(), new animal()); } }
This is wrong. This will produce a compile time error. Since we are not dealing with any inner classes we don't need a instance of Test14. method.invoke (new animal(), new animal()); The above method call is correct and will produce output as : A A
|
Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
|
 |
 |
|
|
subject: Doubt in overloading
|
|
|