• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help on Method Overloading  RSS feed

 
Sanjeev Kumaar
Greenhorn
Posts: 12
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a small program as follow-

output of this program is -

String

please tell me the reason that why it is calling fun(String) when we're not passing any String while calling the function.

Thanks in Advance.

Regards,

Sanjeev
 
Venkatesh Goudampalli
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sunjiv Kumar,

when you call a method by passing a null, it will searchs for the method which has one object parameter.
null can be assigned to any object, so it is executing m.fun(string) method at the above line.

if you want to call a method with no arguments, you need to call like this

and a method with no arguments should be defined like this
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
Please use Code Tags while Posting your Queries

YOur Code will be more understandable and also you will get a good response.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a small program as follow-

public class Main {
public void fun(Object o){
System.out.print("object");
}
public void fun(String o){
System.out.print("String");
}
public static void main(String[] args) {
Main m=new Main();
m.fun(null);
}
}


output of this program is -

String


It is because
String extends OBject and compiler choose most superclasss object when compare to subclass


lets expect what should be the output of this code

see the below link for superclass of integer is
http://download.oracle.com/javase/1.4.2/docs/api/java/lang/Integer.html
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Venkatesh Goudampalli..Welcome to JavaRanch
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:It is because
String extends OBject and compiler choose most superclasss object when compare to subclass

No, it's because the compiler calls the method with the most specific subclass, as the link already provided states.
 
Sanjeev Kumaar
Greenhorn
Posts: 12
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem Resolved

Thanks to all
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good, and welcome to JavaRanch.

Please UseCodeTags next time when you post source code.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:
Shanky Sohar wrote:It is because
String extends OBject and compiler choose most superclasss object when compare to subclass

No, it's because the compiler calls the method with the most specific subclass, as the link already provided states.

That's correct .but tell me whether i am right or not
as per what i think is that most superclass means most specialized class and specialized means most specific class
 
Sanjeev Kumaar
Greenhorn
Posts: 12
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:
David Newton wrote:
Shanky Sohar wrote:It is because
String extends OBject and compiler choose most superclasss object when compare to subclass

No, it's because the compiler calls the method with the most specific subclass, as the link already provided states.

That's correct .but tell me whether i am right or not
as per what i think is that most superclass means most specialized class and specialized means most specific class



You are right if yo'll use most subclass instead of most superclass.........
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!