Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to overload static methods ?

 
Mauricio Lopes
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Reading S&B for Java 5, in the end of chapter 2 there is the following statement:

"static methods cannot be overloaded".

But testing it on my IDE i've written the following valid code:


That's an overloading or am I wrong ?

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[quote=Mauricio Lopes]Reading S&B for Java 5, in the end of chapter 2 there is the following statement:

"static methods cannot be overloaded".

[/quote]

Are you sure it states "cannot be overloaded" and not "cannot be overridden"?
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are misunderstood a little bit here
static methods can definitely be overloaded
but they cannot be overridden
means
you can write static methods with the same name and the same method signature in two different classes but that is not
called as method overriding
this is called as method hiding
as in inheritance the static method defined in the subclass actually hides the method in the super class
burn following rules into the mind
  • Static methods cannot be overridden, that is called as method hiding

  • If super there is a static method in super class called as doStuff(), then in subclass we have to declare the method with name doStuff() as static
    this is mandatory other compilation fails

    the example of method hiding is as follows

    This outputs as follows


    If you are still unclear about the overloading and overriding of methods
    then I recommend you should go through the K & B thoroughly to make it fully understand
    happy preparation
     
    Mauricio Lopes
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That's amazing ! What a great help !

    Oh yeah, it's written in capital letters "MÉTODOS STATIC NÃO PODEM SER SOBRECARREGADOS." (static methods cannot be overloaded). I've got a portuguese version of that book, perhaps it's mistranslated. It says that it can be hidden by another implementation, as you explained.

    Thanks a lot !
     
    Bert Bates
    author
    Sheriff
    Posts: 8900
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    cool!

    I'd love to see a portuguese version of the book!
     
    KrishnaPrasad raghavan
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This topic is interesting. K & B does mention about hiding.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic