• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Overloading methods.

 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers !!!

I have the following code:

public class SCJP {

public static void main(String[] args) {
method(null);
}

public static void method(String obj) {
System.out.println("String");


public static void method(Object obj) {
System.out.println("Object");
}

}

Can someone explain me why the result will be "String" and not "Object" ?
Thanks.!!!
[ August 12, 2007: Message edited by: Collins Mbianda ]
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The compiler matches for most specific one;

String is more specific to Object.

similarly
if you have methods with fallowing class Objects

1.Object

2.A

3.B extends A

then method with B instance will be called for method(null);
 
Collins Mbianda
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Srinivasan thoyyeti.
explaining the whole concept help me to understand well;

thanks
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Just to add.
As to which overloaded method() must be invoked is decided at compile time and not runtime. So if you have below code
class Parent{

}
class Child extends Parent {}

public class Test {
void disp(Parent p) { System.out.println("Parent"); }
void disp(Child p) { System.out.println("Child"); }
}

and you have
Parent p = new Child();
disp(p);
so now since overloading is decided at compile time. At compile time we know that p is of type Parent and can point to any child class object and as a result when you invoke disp(p) the overloaded version that takes Parent is invoked and not the one that takes Child, even if p refers to child object at runtime.
At compile time its decided which version of overloaded method will be invoked
 
What's brown and sticky? ... a stick. Or a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic