Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading" Watch "Overloading" New topic
Author

Overloading

swapna mallipudi
Greenhorn

Joined: Sep 10, 2009
Posts: 11
Will this code compile successfully?

Why the above cann't compile and below can compile? Please explain.

This questions are from Niko's blog.
Thank you.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
for legal overload the argument list must be different which is not the case in first code so it wont compile. But the second code does gives different arg list to methods so it compiles.


SCJP 1.6 96%
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

In the first case you do not have an overloading. return type is not a part of method's signature.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
swapna mallipudi
Greenhorn

Joined: Sep 10, 2009
Posts: 11
Thank you Neha Daga. I missed that part.Got it now.
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
you are welcome
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Lucas Smith wrote:... return type is not a part of method's signature.


I was of the belief that the return type is part of a method signature, it may not be necessary for overloading but it is certainly vital in overriding? However if this is not the case I stand corrected.
in overloading at the very least the argument list must be different. In the example given both methods have the same arg type, one takes an Array of Strings and another a var-arg of strings which is actually a wrapper for an array object and is converted at runtime to an array object, so the arguments are the same.

However the second is a reasonable overload because there are two arguments provided to the method signature not one, and hence this fulfills the requirement of overloading.


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Stephen return type is not a part of method signature. Look at this or this, I'm quoting a line from the second link
Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Ankit Garg wrote:Stephen return type is not a part of method signature. Look at this or this, I'm quoting a line from the second link
Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.


Ankit, in your second link to the Sun java tutorials it actually says:

The only required elements of a method declaration are the method's return type, name, a pair of parentheses, (), and a body between braces, {}.


So I see my confusion here, its not the signature but a declaration I was thinking about.

Thanks for the heads up!
jose chiramal
Ranch Hand

Joined: Feb 12, 2010
Posts: 266
In the below program am trying to understand two things :

class Overload {
public void method(String[] names) {
}
final Object method(String name, String... names) {
return null;
}
}



1. We return null , wherein an Object should be returned. However it doesnt give me any error. Is null an object ?
2. Why do i get duplicate method error. Since the retrun type is also part of a method. Shouldn't we ahve two different methods above ?

Thanks.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
jose chiramal wrote:In the below program am trying to understand two things :

class Overload {
public void method(String[] names) {
}
final Object method(String name, String... names) {
return null;
}
}



1. We return null , wherein an Object should be returned. However it doesnt give me any error. Is null an object ?
2. Why do i get duplicate method error. Since the retrun type is also part of a method. Shouldn't we ahve two different methods above ?

Thanks.


Jose,

1. As null is a "reserved constant used in Java to represent a void reference( ref )", providing you are not trying to act upon the reference (i.e call any methods on the null reference) it is fine to return it from a method expecting any object as a return type, without error. You would only get the infamous null pointer exception if you then tried to use that returned reference.

2. You are getting a duplicate error as you are declaring one method as Final, which means it becomes a constant and cannot be reassigned anywhere else so in this sense you are declaring the method 'method' as a constant which means even in overloading you cant assign it to another method (such as your void one) called method. Also as I have recently discovered the return type is only part of a method declaration not signature. IN overloading you only must change the argument list, you can have different or the same return types

see here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading
 
Similar Threads
Method Overloading question
Confusion with the below mentioned code
I got this question from a mock Exam!
overloading
Help Me...Doubt in Overloading..