class C extends B { static void doSomething ( A x , A y ) {System.out.print("AA");} static void doSomething ( A x , B y ) {System.out.print("AB");} static void doSomething ( B x , A y ) {System.out.print("BA");} static void doSomething ( B x , B y ) {System.out.print("BB");} static void doSomething ( C x , C y ) { System.out.println("CC");}
public static void main(String[] args) { doSomething(null,null); } }
Here the o/p is "CC". How this method overloading takes place? and how the method called with (null,null) will run the last method while all r eligible methods? can anybody plzz explain?
Thanks,<br />Swati Thorve<br />SCJP 1.4
Lakshmanan Arunachalam
Ranch Hand
Joined: Nov 02, 2005
Posts: 99
posted
0
This is because in runtime JVM checks for what all are eligible methods that can be invoked by the given method call. If it finds more than one eligible methods, then it chooses the method which can be safely called. i.e Here in this case, method which accepts Object of type C is called.
Because Object C is of type B and also of type A. If you break the inheritance hierarchy, then you will get compile time err.
wat i thought was it is something to do with stack mechanism
as the method is overriden the last in method is the first stack method and hence the output.
plz do correct me if i am wrong.
Harish<br />SCJP 1.4 (85%)
Aryaan Rajvansh
Greenhorn
Joined: Jan 27, 2006
Posts: 5
posted
0
Hi Swathi !!!
sometime back i had a similar doubt with a question.To answer your doubt i am putting my question, its answer and the explaination. Hope you would be yourself able to understand the analogy.But if you dont..feel free to ask me again..
What is the result of attempting to compile and run the program? a. Prints: Object b. Prints: String c. Compile-time error d. Run-time error e. None of the above
Answer is b Prints String and here is the explaination.
A method invocation conversion can widen an argument of type String to match a method parameter of type Object, so any argument that can be passed to m(String x) without generating a compile-time type error can also be passed to m(Object x). For that reason, we can say that m(String x) is more specific than m(Object x). The argument of the method invocation expression, m(null), is of type null and can be converted to either type String or Object by method invocation conversion, so both methods, m(String x) and m(Object x), are applicable. The more specific of the two, m(String x), is chosen over the less specific, m(Object x).
I think that should solve your doubt.
ven kaar
Ranch Hand
Joined: Nov 01, 2005
Posts: 39
posted
0
I think Harish answer is not convincing as, if u change the order of method still "CC" is the result if u run the below
I also think Lakshman answer , if u break the inheritance it will not compile, its agreed it will not compile, i think the reason is bcos of ambigous method call, to substantiate that
for the above "AB" is output
for the above, it will not compile bcos -ambigous
so my answer is in line with Rajvansh
Parallax - Change in observational position that provides a new line of sight.
vandu matcha
Ranch Hand
Joined: Dec 28, 2005
Posts: 57
posted
0
yes..as aryan just said that..most specific one is called and is executed...
Anju sethi
Ranch Hand
Joined: Dec 26, 2005
Posts: 91
posted
0
I am unable to understand the Solution. Can anyone explain it more.
I am confussed in the last example with two overloaded methods.
Please explain
thanks,<br />Anju Sethi
satya mahapatra
Ranch Hand
Joined: Jan 07, 2006
Posts: 134
posted
0
this is called Method OverLoading resolution.given in page 277 of khalid m. in this type of question just call the more specific method or in other word that method whose parameter represnts the class which is at the BOTTOM of class heirarchy.
Regards,<br />Satya<br />SCJP,SCWCD
Anju sethi
Ranch Hand
Joined: Dec 26, 2005
Posts: 91
posted
0
...hmm. i read some docs. thanks I got it
Tilo Hemp
Ranch Hand
Joined: Nov 21, 2005
Posts: 91
posted
0
it's interesting that this is a compile-time only thing:
prints "Object Object", although the method is actually called with two strings [ January 30, 2006: Message edited by: Tilo Hemp ]
amrita sankar
Greenhorn
Joined: Jan 25, 2006
Posts: 26
posted
0
If the code is like this i.e instead of Object ,String is there ,string args ll b called and output ll b string
public class Hai1 { public static void main(String[]puppets) { String s = "string"; x(s, s); }
public static void x(Object o1, Object o2) {System.out.println("Object Object");
Hello All, There is something amazing happening here, if i write the code like
I get the output as Object Object Please clarify whats happening here. Regards, Seema
John Brown
Ranch Hand
Joined: Dec 01, 2004
Posts: 35
posted
0
What happens here is what is expected to happen, you're calling method x with two Object references, so the right method executes - x(Object o1, Object o2)
Tilo Hemp
Ranch Hand
Joined: Nov 21, 2005
Posts: 91
posted
0
hi simi,
i posted the code because it was also interesting for me. the point is: it is not important of which type the parameters are at runtime. it is only important, which object reference is compiled. and in the code, the parameters are of type Object at compile time.
i first thought, method invocation would be done depending on an "instanceof" query, which is not the case.
greetings tilo
steven gerrard
Ranch Hand
Joined: Jan 21, 2006
Posts: 55
posted
0
declaration Object string = "string"; is equivalent to String s = "string" Object string = (Object)s
remember that which method will be called is decided at compile time and not run time and hence method with Object as arguments is called
Salvador Cecilio
Ranch Hand
Joined: Dec 20, 2004
Posts: 41
posted
0
As I understand it and I did see this on a mock exam, when several overloaded methods are compatible with the method call, the runtime will choose the one with the most specific parameters (class C) as opposed to the more general (class A).
SCJP 1.4 - 93%
SCWCD 1.4 - 89%
IBM FileNet 3.5 - Developer
IBM FileNet 3.5 - Administrator