Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Overloading

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Got yaa!!
    Thanks Jeff and Calum!!
     
    Ranch Hand
    Posts: 133
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Marshal
    Posts: 74069
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Ranch Hand
    Posts: 451
    1
    IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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?
     
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 74069
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Sheriff
    Posts: 13411
    Firefox Browser VI Editor Redhat
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    "Raj Kamal R",
    Please check your private messages regarding an important administrative matter.
    -Ben
     
    Rajkamal Pillai
    Ranch Hand
    Posts: 451
    1
    IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 74069
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
    Thread Boost feature
    https://coderanch.com/t/674455/Thread-Boost-feature
    reply
      Bookmark Topic Watch Topic
    • New Topic