• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading

 
jaspreet atwal
Ranch Hand
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
Jeff Storey
Ranch Hand
Posts: 230
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

  •  
    jaspreet atwal
    Ranch Hand
    Posts: 52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Got yaa!!
    Thanks Jeff and Calum!!
     
    Praveen Kumar
    Ranch Hand
    Posts: 133
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 47300
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Calum Ludwig gave the best answer. The return type is ignored in working out whether methods are overloaded or not.
     
    Rajkamal Pillai
    Ranch Hand
    Posts: 445
    1
    Java Spring
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 31
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 47300
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 13411
    Firefox Browser Redhat VI Editor
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    "Raj Kamal R",
    Please check your private messages regarding an important administrative matter.
    -Ben
     
    Rajkamal Pillai
    Ranch Hand
    Posts: 445
    1
    Java Spring
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 47300
    52
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic