• 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
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Why the interviewer challenged me?

 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Recently i was interview for a job. The interviewer asked me whether static methods get inherited? I said yes they do! He said, "really? try it"

What was I missing?
For example:

Parent class


Child class



compiles easily and that static and non-static methods are called without any problem.

Any comments?

Maki Jav
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what he meant is that they don't work polymorphically*, as instance methods do. In that case, I think you answered correctly and he made a poor, or ambiguous, choice of words.

(*) As in overriding the static method, having a supertype reference pointing to a subtype object, and making the subtype method execute invoked with the supertype reference.
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh you mean...



I have not tested it but will it work? Comments?

Thank you,

Maki Jav
[ September 25, 2007: Message edited by: Maki Jav ]
 
Author
Posts: 976
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Maki Jav:
Recently i was interview for a job. The interviewer asked me whether static methods get inherited? I said yes they do! He said, "really? try it"

What was I missing?



The term "inherited" is somewhat ambiguous. The child class can of course call the parent's static method (without the "ParentClass." prefex), so it is inherited in that sense.

What the interviewer was trying to ask is if static methods can be overridden. (They can't.) Simple example:



In this case ChildClass has its own staticMethod() which shadows (but doesn't override) the method of same name in ParentClass. (I prefer the term shadow but some use the term hide.)

The point is that while it is legal to write "foo.staticMethod()" it doesn't really make sense to do so. It's better to write either "ParentClass.staticMethod()" or "ChildClass.staticMethod()" since that's how the compiler handles it anyway.

[edit: I see your Albert/Human example is similar. It's not that human.getQuantity() "does not work", just that it is a call to the static method in the Human class, not in the Albert class. Since getQuantity() is static, it's the declared type of human that matters, not the actual type that is referenced at runtime.]
[ September 25, 2007: Message edited by: Brian Cole ]
 
Let's get him boys! We'll make him read this tiny ad!
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic