Results 1 to 19 of 19

Thread: Overloading qn

  1. #1
    diskhub is offline Member
    Join Date
    May 2010
    Posts
    25
    Rep Power
    0

    Default Overloading qn

    Does java supports function overloading with different return type?
    i know different return type with same parameters are not allowed.
    but can different return type with different signature allowed and considered overloading?

  2. #2
    svidurs1 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    I would say no.
    Method signature in java is composed of following elements:
    Modifier,
    Return type,
    Method name,
    parameter list,
    Exception list.
    Method overloading in java talks only about the difference in the number of parameters rest everything in the method signature has to be the same.

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by svidurs1 View Post
    Method overloading in java talks only about the difference in the number of parameters

    and the type of parameters. so this two methods are considered overloaded

    public void method1 (int, double)

    public void method1 (double, int)

    but you're right. the return type is not considered for overloading.

  4. #4
    svidurs1 is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default

    yeah forgot to add that.. parameter type also counts !! Thanks budddy for adding that >>

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    What if you tried
    Java Code:
    public int method1(int a, int b)
    public double method1(int a, int b) //or maybe (double a, double b)
    Instead of having to typecast a result you just have "one" method that does the same thing but is able to determine if what get's returned is being used as in context.

    so like Double brownChickenBrownCow = method1(a, b) would be able to distinguish you needed a double and as such would call the second method1(the one returning a double)?
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Here's a full example to show what I mean. It's only a simple proof of concept but lays out the foundation.

    Java Code:
    public class Testing {
    	public static void main(String[] args) {
    		
    		//should realize c is an int and call the int method1
    		int c = method1(1, 2);
    		
    		//should realize d is a double and call the double method1
    		double d = method1(1,2);
    
    	}
    	
    	public static int method1(int a, int b){
    		return a + b;
    	}
    	
    	public static double method1(int a, int b){
    		return (double)(a + b);
    	}
    
    }
    It won't compile because it says the methods have the same name why couldn't we overload in this fashion? I can't believe for a second I'm the only person that has seen how useful this could be.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Quote Originally Posted by xcallmejudasx View Post
    What if you tried
    Java Code:
    public int method1(int a, int b)
    public double method1(int a, int b) //or maybe (double a, double b)
    The compiler should throw up and not compile the source. You cannot have duplicate method signatures. From my understanding, in java a method signature is really just the method name, and the number of parameters and their respective types.

  8. #8
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    Quote Originally Posted by xcallmejudasx View Post
    Here's a full example to show what I mean. It's only a simple proof of concept but lays out the foundation.

    Java Code:
    public class Testing {
    	public static void main(String[] args) {
    		
    		//should realize c is an int and call the int method1
    		int c = method1(1, 2);
    		
    		//should realize d is a double and call the double method1
    		double d = method1(1,2);
    
    	}
    	
    	public static int method1(int a, int b){
    		return a + b;
    	}
    	
    	public static double method1(int a, int b){
    		return (double)(a + b);
    	}
    
    }
    It won't compile because it says the methods have the same name why couldn't we overload in this fashion? I can't believe for a second I'm the only person that has seen how useful this could be.
    I'm not so sure it would be as useful as you think, but the reasoning behind it is that the JVM would not know what method to call as you can assign an int to a double, so both methods are valid for double d = method1(1,2);.

    Usually when I have something like you demonstrated where both parameters are of the same type, I have the method return the double, and if needed, typecast it to an int. However you can have a second method defined where the parameters are of different types.
    Last edited by StormyWaters; 06-04-2010 at 10:46 PM.

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Maybe I'm just pulling at straws here but what about for more complex objects? Say you were designing some code for DNA manipulation or something and you had a method called mutate(well a few methods called mutate, each with different sets of parameters)

    *Please don't destroy me for my lack of biological knowledge it's been years since I've seen a chemistry/bio book*

    public static aquatic mutate(RNA, Sugar, Mutagen, etc)
    public static terra mutate(sugar, spice, everything nice)
    public static avian mutate(flip, flop, tik, tok)

    Say you start off with a simple object (Cell) and call the mutate method with randomly generated parameters. You don't know what parameters you're going to receive but you can determine the output type based on the parameters sent in. So when "creating" these lifeforms(all totally hypothetical and used just to get a point across before anybody gets offended) what you put in is not what you get out. You won't know what you're getting until it's done.

    So something like
    Java Code:
    if(cell.mutate().type == Avian){
    	habitat = Sky;
    }else if(cell.mutate().type == Terra){
    	habitat = Land;
    }else if (cell.mutate().type == Aquatic){
    	habitat = Water;
    }else{
    	//new species developed
    }
    I know the JVM doesn't currently allow this type of code but why? I don't pretend to understand everything that goes down at Sun but this seems like a legitimately viable and powerful tool they could implement.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    With your example what version of the method should be called on the line below?
    Java Code:
    cell.mutate();

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by diskhub View Post
    Does java supports function overloading with different return type?
    Methods with identical names must have different parameter type lists and can have different return types. They are all overloaded methods.

    kind regards,

    Jos

  12. #12
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    In this case, I would have it return an Abstract Class which all life forms/types would extend. Inside this abstract class would be methods called isAvian(), isTerra() and isAquatic() which would be used to determine such things and if needed I would typecast it into the more specific object.
    Java Code:
    public abstract class LifeForm {
           
           //Overwritten by AvianLifeForm(extends LifeForm) to true
           public boolean isAvian() {
                  return false;
           }
    
           //Overwritten by TerraLifeForm(extends LifeForm) to true
           public boolean isTerra() {
                  return false;
           }
    
           //Overwritten by AquaticLifeForm(extends LifeForm) to true
           public boolean isAquatic() {
                  return false;
           }
    }
    What if they did things that are completely different like one alters a database table while another one just displays information to the screen? Just because a method is named the same with the same parameters and return type doesn't mean the logic would be the same or what they return would mean the same thing.

  13. #13
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Nothing now it's all theoretical and there's no default constructor. Thinking about it though I'd try to have something like.

    Java Code:
    public *type* mutate(){
    	//randomly generate DNA sequences
    	if(sequence is Terra based){
    		return Terra object;
    	}else if (sequence is Aquatic based){
    		return Aquatic object;
    	}else if (sequence is Avian based){
    		return Avian object;
    	}
    }
    So a program designed where either the same method can have multiple return types, or multiple methods with the same name but different return types.

    Forgive me if this is backwards I'm really tired. But from an inheritance point of view say
    Species-
    -Terra
    -Aquatic
    -Avian

    Would the mutate method be setup like
    public static Species mutate()....so that a method could return a subclass of it's return type instead of having to define it exactly?

    Maybe what I'm imagining is just shortcutting some AI decision making code. It just seems inefficient to have 3 different methods for the exact same code just with different objects in the end.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    If you find yourself needing lots of isWhatever things like that, essentially standing in for instanceof, I would question your design because that's often a sign your OO is broken somewhere.

  15. #15
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    @StormyWaters, That came to mind originally and I guess the reasons you stated are why it's not set up to work how I've been explaining. This was all just kinda posted as it came to me, it's not like I was actually trying to implement anything in that fashion, just provoked some thoughts and I wanted to explore the possibilities.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    As I tried to point out above, the reason is because you do not have to do anything with a return parameter in Java (or any C-based language), so there is nothing in those cases for the compiler to work with. You'd have to change one of the fundamental ways the language works.

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by xcallmejudasx View Post
    @StormyWaters, That came to mind originally and I guess the reasons you stated are why it's not set up to work how I've been explaining. This was all just kinda posted as it came to me, it's not like I was actually trying to implement anything in that fashion, just provoked some thoughts and I wanted to explore the possibilities.
    A Method can only have one return type, i.e. it can't return a T1 as one call and a type T2 at another call. Method overloading also is just a compiler issue, not a late binding (at runtime) issue so the compiler knows what type a certain method returns. There is no magic in it.

    kind regards,

    Jos

  18. #18
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Sorry Tolls you posted while I was adding my response lol.

    I guess my desire for minimal work but maximum efficiency has a bad habit or me missing the larger picture on why things are setup how they are.
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default

    You are not alone in asking this question, believe me.
    It comes round a lot.

Similar Threads

  1. Overloading abstract methods??!?
    By tfitz666 in forum New To Java
    Replies: 2
    Last Post: 03-07-2010, 09:45 AM
  2. method overloading and arrays.
    By glopez09 in forum New To Java
    Replies: 5
    Last Post: 12-08-2009, 03:02 PM
  3. Overloading?
    By padutch2 in forum New To Java
    Replies: 2
    Last Post: 12-31-2007, 03:26 AM
  4. Method/Operator Overloading
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:33 PM
  5. Overloading methods dynamically
    By Syed.muddasir in forum New To Java
    Replies: 6
    Last Post: 07-31-2007, 02:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •