Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

null as a parameter to method  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have following code

public class Tester
{

//main method
public static void main(String[] args)
{
painter(null);///look here
}

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

public static void painter(String str)
{
System.out.println("String method");
}

}


now I see "String method" as output for this program , on which basis String parameter method is called while I am passing null as parameter?
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here you have method painter() which you have overloaded in the same class Tester. Generally the term overloaded is used in case of inheritance, that is when we have a base class and a sub class, and than in the sub class we overlaod the method from the base class. Anyways,

Generally in the case of an overloaded method, the more specific of the two methods is selected. In your case the parameter which you are passing to your methods is "null".

Now, String is a subclass of Object, therefore "null" is more applicable to String rather than Object class.

Lets take another example to clarify it, lets replace "null" by a literal "8". Refer to the code below:



The output of this code will be Integer selected, although both the painter methods are applicable, the integer method is selected becuase it is more specific of the two.
 
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that the "most specific" method is chosen. That (essentially) means that the class that is most removed from the Object class. What happens if you have two classes that are equally specific?


You get this error:
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Generally the term overloaded is used in case of inheritance, that is when we have a base class and a sub class, and than in the sub class we overlaod the method from the base class. Anyways,


Replace the words "overloaded" with "overridden" and tell us if what you originally said is correct.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output of this code will be Integer selected, although both the painter methods are applicable, the integer method is selected becuase it is more specific of the two

Actually, it is because you are passing an integer. Specificality doesn't apply to primitives, as they do not extend one another. There's no confusion to the compiler because a literal 8 is always and forever an integer. To get a literal 8 as a long, you owuld have to use 8L.
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Catch your point guys, thanks, sorry for using the wrong terminology Sandeep.
 
Sandip Shinde
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for great reply from u all,
Got the exact basis for overloded methods invocation(more specific one will be called .. clear enough)

Thanks again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!