• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference in writing overriden methods and overloaded methods.

 
pagano monello
Ranch Hand
Posts: 38
1
Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it correct to think, making an extreme synthesis, that an evident difference between an overloaded method and an overridden method is that: an overloaded method must have a different signature compared to that of the method that it overloads, whereas an overridden method must have the same signature of the method that it overrides?

I don't want to trivialise, I am just trying to simplify things to approach to the real difference between overloading and overriding.

Thank you in advance.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pagano monello wrote:I don't want to trivialise, I am just trying to simplify things to approach to the real difference between overloading and overriding.

If we compare both on method name and parameters, then
  • overloaded = same method name with different parameter list
  • overridden = same method name with same parameter list


  • If you add access modifier, return type and exception list into the comparison, then
  • overloaded: return type may be different, access modifier may be different, exception list may be different
  • overridden: return type must be same (besides the covariant return type), access modifier must be same or less restrictive, exception list: checked exceptions must be same, narrower or none (newer or broader checked exceptions are not allowed); unchecked exceptions may be different


  • In short: an overloaded method must have exactly the same name but the parameter list must be different. An overridden method must have exactly the same name and must have exactly the same parameter list.

    Hope it helps!
    Kind regards,
    Roel
     
    pagano monello
    Ranch Hand
    Posts: 38
    1
    Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Roel, great answer.

    Thank you.
     
    Campbell Ritchie
    Sheriff
    Posts: 51366
    87
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Also:-

    Only non‑private instance methods can be overridden. If you think you are overriding a method always write @Override before it. Like thisIncorporate both those bits of code in a class and see what happens when you try to compile them.
     
    pagano monello
    Ranch Hand
    Posts: 38
    1
    Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Ritchie,
    thank you for your explanation and hint.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic