Why not write a class with these methods, compile it, and see what happens? Using the JDK yourself is one of the best learning tools avilable to you. We can certainly help with questions like "why doesn't this work" or "how does this work", but we don't want to do everything for you.
the editor show this error message btw i m using bluej to learn java so the error message is from bluej the error msg MyWeight(int,java.lang.String) is already defined in overload public void MyWeight(int dest,String s)
That's exactly the point. The compiler cannot distinguish between the two methods if they have the same signature ( the list of parameter types ) but have different returntypes.
This will not compile. The types of the parameters correspond one for one. int - int, String - String. But the return types differ: int - void If you have the following code:
Which method would the compiler call? the one returning an int, or the one returning nothing (void)? Result: compiler meltdown. Overloading occurs only when you have a difference between the lists of types in the parameter lists. A difference in number of parameters, or a difference in the correspondence of the parameters. The return type (and the method name, of course) must remain the same. By the way, when BlueJ displayed the error message, did you press the "?" button? Sometimes you get more details as what the problem is caused by. [ March 16, 2003: Message edited by: Barry Gaunt ]
So the moral of the story is that if the parameters in the parentheses are identical (and in the same order) and the name of the method is identical, the compiler thinks it is the same method (not an overloaded method) even if the return type is different.
In order to be overloaded, the parameters must be different while the method name remains the same. (jinx, Barry) [ March 16, 2003: Message edited by: Marilyn de Queiroz ]
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
..this protected private what does it actually do? is it the same as private?? You can likely ignore that old combined modifier. It's no longer supported in Java. Today, we've four levels of access modification to be concerned with: private, default (no specifier), protected, and public. If it's private, it may be accessed from within the same class only. If it's default (also known as package), then it may be accessed by any class in the same package. If it's protected, then it may be accessed by any class in the same package or any class that is a subclass. If it's public, any class may access it. Take a look at Sun's Java Tutorial on Controlling Access to Members of a Class.
it does compile, i did not get any errors .you are saying thats not the way?? If it compiles, it's allowed. If you're implementing this in a real application, I would suggest that you might want to consider a different interface. Having two methods with the same name and the same arguments (though in different order), but with different return types is a bit odd and would likely cause other programmers using this class to stop and think too hard. [ March 17, 2003: Message edited by: Dirk Schreckmann ]