Wrox Programmer Forums
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 2nd, 2005, 02:38 AM
Authorized User
 
Join Date: Aug 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to olupas
Default Why that...?


Hi all...
I have an interesting questing..,
Suppose U have a class with 2 overloaded method like this:

 public void omethod(String param){
  System.out.println("from string");
 }
 public void omethod(Object obj) {
  System.out.println("from object");
}
Ok...from main when u call omethod(null) you get the message from string

Anywone knows why?

LOOP


 
Old August 4th, 2005, 04:02 AM
Authorized User
 
Join Date: Aug 2004
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to olupas
Default



Oki doki

I found the answer.Here it goes....

Java would every time try and call the most specific method instead of generalized one. String is more specific (lower in inheritence chain) than object, and the fact that null is assignable to String makes the method taking String called.

Try putting in another method which takes a Hashtable or an Array(or anything else for that matter) and you will get a compile time error.

The order of the methods does not make any difference.

Thanks to java.forum











Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.