• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Overriding static methods

 
Greenhorn
Posts: 21
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe my question hinges on the definition of "override".

I know the override rule about static methods to be as follows:
  • static methods cannot be overridden by non-static methods
  • non-static methods cannot be overridden by static methods


  • So, okay. We can override a static method with another static method. Great. But in doing so we lose the polymorphic behavior because the overriding static method only "hides" or "shadows" method in the super class.

    Consider the following code:


    We do not get the TestThisClass behavior in the ov.write() line. I understand this is because the ov reference variable is of type TestOvClass, and since the write method is static on that class, the JVM executes write as if it were TestOvClass.write() instead of running the method on the TestThisClass object the variable is referring to.

    First, am I thinking of this in the right way?

    Next, can we still say the method is overridden even if we do not get the polymorphic behavior? Is overriding merely a question for the compiler? If so then we can say yes, the compiler allows the override. Or is override a question for execution where we expect to benefit from polymorphism?

    I can just imagine an exam question that would ask if the write method is overridden in TestThisClass. My response at this point is "yes, and no".

    Thanks!
    Doug
     
    Bartender
    Posts: 1952
    7
    Eclipse IDE Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Referring to this as overriding is indeed not correct. The correct term would be hiding, as you said.
    Your response should be a resounding "no".
     
    Sheriff
    Posts: 11604
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Overriding ONLY applies to instance methods, NOT to variables or static methods. But you can hide/shadow variables and/or static methods.

    Douglas Cyporyn wrote:I can just imagine an exam question that would ask if the write method is overridden in TestThisClass. My response at this point is "yes, and no".


    Just select the "No" radio button (because you learnt at CodeRanch that only instance methods can be overridden ) and you can confidently move on to the next question!
    reply
      Bookmark Topic Watch Topic
    • New Topic