DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Does Java have polymorphism for static methods?

  1. #1
    Join Date
    Nov 2002

    Question Does Java have polymorphism for static methods?

    I am experimenting with polymorphism for static methods.
    There is something surprising me:


    public class Parent {

    public static String getString(){
    return "Parent";

    public static String getYourString(){
    return getString();


    public class Child extends Parent{

    public static String getString(){
    return "Child";


    Child.getString(); // returns "Child"
    Child.getYourString(); // returns "Parent"


    Could anyone explain me such a feature?

    Thank you.

    Alexander lykov

  2. #2
    Join Date
    Dec 2002
    All the parent's methods are accessible to it's child. Therefore, since getYourString() is NOT overridden in your child class, it calls getYourString() of it's base class, which is it's parent's getYourString method.

    what you are seeing is a polymorphism issue, not a static method or not issue.

    hope that helps

  3. #3
    Join Date
    Nov 2002


    Dear GunNam,
    Thank you for reply.

    But it is issue of static methods only.
    Remove word "static" from method declaration and you will have "child"
    as a return values for both cases.
    I did not meet any remarks about different polymorphyc behaviour for
    static and not-static metods in books. It is why I was surprised.
    Thus, I still do not have clarity on this matter.

    Sincerely, Alexander Lykov

  4. #4
    Join Date
    Dec 2002

    Ordinarily, when classes are declared, we get nothing except storage for a handle until an object of that class is created with "new." At that point, data storage for the object is allocated and methods and data elements become available.

    Using "static" keyword signifies that it is not tied to any object instance of that class, so even if we never create an object of that class, we can call a static method or access a piece of static data.

    Having said that:
    What is happening is that when the "static" declaration IS used, although getYourString() is called from the Child class, it DOES NOT "not tie it to any object instance of that class," meaning the Child class that called it. Therefore you get the parent's "getString()."

    On the other hand, if "static" declarations are not used, calling Child.getYourString() DOES TIE it to the Child class, and therefore calls the "getString()" of the Child class that called it, which is overridden from the Parent Class.

    Hope that clears things up a bit, it did for me.

  5. #5
    Join Date
    Sep 2011


    I know, it has been 9 years since the last post, this is for anyone doing a Google search like me.

    I found the response worthless and I have a much simpler explanation.

    In Java, there is ALWAYS an implicit "this." if you are calling a method without an object in front of it.

    So in the parent's method of getYourString, when you compile and run, it basically converts the method call of "getString()" to "this.getString()" Because it is a static method, the "this." gets converted into "Parent.getString()" and voila, that is why the getString() of Parent is being called via statics as opposed to under the child.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center