• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overloading

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This Q is from Voodoo exam

class A { }

class B extends A { }

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?
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,

is it so as Lakshmanan told..

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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..

class GFC218 {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {
m(null);
}}


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.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes..as aryan just said that..most specific one is called and is executed...
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anju sethi
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...hmm. i read some docs. thanks I got it
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");

}
public static void x(String s1, String s2)
{System.out.println("String");
}
}
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic