File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes Why the interviewer challenged me? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Why the interviewer challenged me?" Watch "Why the interviewer challenged me?" New topic
Author

Why the interviewer challenged me?

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
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


Help gets you when you need it!
greg buela
Ranch Hand

Joined: Sep 04, 2007
Posts: 71
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.


SCJP 1.5
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 435
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 ]
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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 ]

bitguru blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why the interviewer challenged me?
 
Similar Threads
overriding static methods
Static methods can't be overridden?
Polymorphism applies to instance methods not instance variables?
overloading static methods?
Using static class