This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java 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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overloading" Watch "Overloading" New topic
Author

Overloading

jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
For the following code why would Compiler throw an Error, why won't it be considered an overloading?


method() in Test cannot override method() in parent; attempting to use incompatible return type
found: float
required: int




Still Learing..
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Jaspreet,

What you are actually trying to do is overriding a method. Overloading is a method in the same class with a different set of arguments. When you override, you must have the same method signature, return type too. Imagine the following code:



hope this helps.

Jeff
Calum Ludwig
Greenhorn

Joined: Oct 01, 2007
Posts: 14
Overloading has more to do with the method parameters.

Below is something I gleaned from a site:
  • overloaded methods can have the same name but must have different parameter lists
  • parameter lists are considered different if the order of the arguments are different
  • a subclass method can overload a superclass method


  • SCJP 1.4<br />SCJD
    jaspreet atwal
    Ranch Hand

    Joined: Sep 05, 2007
    Posts: 52
    Got yaa!!
    Thanks Jeff and Calum!!
    Praveen Kumar
    Ranch Hand

    Joined: Nov 06, 2006
    Posts: 133
    The no of args should differ for overloaded methods basically.
    That means overload methods have different return types in addition to this they should[must] differ in args.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    Calum Ludwig gave the best answer. The return type is ignored in working out whether methods are overloaded or not.
    Rajkamal Pillai
    Ranch Hand

    Joined: Mar 02, 2005
    Posts: 443
        
        1

    IMHO,

    In the case of overloading how could the compiler identify which function needs to be invoked (in case the parameter lists are not different like the cases Ludwig mentioned)? The two functions mentioned in the sample code has same arguments(none) so the compiler will not be able to figure out which one of the two you intend to invoke?
    Nidhi Jain
    Ranch Hand

    Joined: Mar 26, 2006
    Posts: 31
    The complier willbe invoked the function,that class have the refrence variable.

    Suppose if I have
    clas A
    {
    void try()
    {
    // some code is here;
    }
    }
    class B extends A
    {
    int try()
    {
    // some code is here;
    }
    }

    public static void main(String args[])
    {
    A a=new A();
    B b=new B();
    A ab;
    ab. try()

    }

    So that will invoke the super class function please clear if i am wrong....
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    Afraid I don't understand your code, Nidhi Jain.
    You will get at least 3 compiler errors: two for use of "try" as an identifier and one for not initialising the local variable ab.
    Ben Souther
    Sheriff

    Joined: Dec 11, 2004
    Posts: 13410

    "Raj Kamal R",
    Please check your private messages regarding an important administrative matter.
    -Ben


    Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
    Rajkamal Pillai
    Ranch Hand

    Joined: Mar 02, 2005
    Posts: 443
        
        1

    Hi Campbell,

    Since you read my reply to the original question, what is your take on the point I mentioned? What I was talking was not about the object not having the reference but how could the compiler choose the correct function to be invoked if just the return types are different. It can be done by using reflection to find the type of the variable the result of the function is getting assigned to. But I guess thats why languages don't allow overloading based on return types alone.

    Do you think I am right?

    Cheers,
    Raj.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38075
        
      22
    You are correct. If you allowed overloading on return types and required reflection to sort them out, think how slowly the language would execute.

    We ahve been studying the concept of refinement recently, and that has the result that an overridden method must be refined by its overriding methods. That can only happne if they have compatible return types. If you have int method() in the superclass and double method() in the subclass, you can no longer say the subclass IS-A superclass.
     
    wood burning stoves
     
    subject: Overloading
     
    Similar Threads
    why does this not compile?
    overriding
    overriding / overloading
    Overidding Methods
    Overload methods in subclass?