Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By jim829
  • 1 Post By jim829

Thread: Why can't we override a STATIC method?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Why can't we override a STATIC method?

    Why doesn't Java let us override static methods?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why can't we override a STATIC method?

    Well, if you could, they wouldn't be very static, would they? :) Ok, so much for my attempt at humor.

    You can only override inherited methods. Static methods are not inherited.

    Consider what happens when you override an instance method. If the parent class calls that method, the overridden
    method in the child will be called. If you want to call the parent version, you use the super keyword within the
    child method.

    But it doesn't make sense for static since it is a class (static) method and is not tied to any particular
    instance of the class.

    Note that you can hide the static method with your own version. But the parent will still call its own.

    Regards,
    Jim
    Last edited by jim829; 06-04-2016 at 03:46 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Why can't we override a STATIC method?

    Static methods are not inherited.
    But from here,

    A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members

  4. #4
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Why can't we override a STATIC method?

    Consider what happens when you override an instance method. If the parent class calls that method, the overridden
    method in the child will be called. If you want to call the parent version, you use the super keyword within the
    child method.

    But it doesn't make sense for static since it is a class (static) method and is not tied to any particular
    instance of the class.
    It sounds like the issue of calling static members from an instance's context. Is it? But it is legal to call a static method using an instance's name with dot notation.

    Java Code:
    class Animal {
      static void eat() {
        System.out.print("Animal eating!");
      }
    }
    
    class Cat extends Animal {
      @Override
      static void eat() {
        System.out.print("Cat eating.");
      }
    }
    
    class MainClass {
      public static void main(String [] args) {
        Animal animal= new Cat();
        animal.eat();
      }
    }
    I know the Cat class won't compile. But hypothetically speaking, what would go wrong if it did compile? It would print

    Java Code:
    Cat eating.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why can't we override a STATIC method?

    First, I was mistaken about not inheriting static methods.

    Since static methods are not associated with a particular instance they cannot reference object state. So they basically serve
    as a functional methods (like those in the Math class). But when overriding instance methods, one can call the parent by
    prefixing the super keyword to the method name. This allows the parent method to set state appropriate to the parent class that
    the child does not know about. I can't think of a good reason to have the parent call the child's version of the parent's static method.

    Note that as long as access procedures are followed (including final) that a child may hide a parents static method with
    its own.

    Regards,
    Jim
    Last edited by jim829; 06-04-2016 at 07:39 PM.
    Zarah likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Why can't we override a STATIC method?

    Theoretically the designers of the Java language could've made static methods inheritable; normal methods are put in a VFT (Virtual Function Table, this is C++ lingo); the linker fiddles a lot with those tables in order to populate those tables with the correct functions/method pointers. The same could've been done per class for the static methods. The designers of the language decided otherwise because inheritable static methods don't buy you much and they even can be dangerous (what will happen when you think you're calling a static method but you're calling an inherited method instead?)

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why can't we override a STATIC method?

    More to Jos's point about the designers could have done certain things. Your questions are no different than
    why doesn't Java have structures, unsigned types, or overloaded operators. Or why not in certain cases,
    implied references. There are times when it would have been convenient to do something like the following:

    Java Code:
    int [] array = {1,2,3,4,5,6};
    for (int b : array) {
        b *= 10;  //multiply each array element by 10
    }
    In Perl there is a similar construct. Except b acts as a reference to the array element. So with no additional syntax,
    you can alter the array. In Java, b simply contains a copy of the value.

    My guess is that the designers wanted to compromise between a relatively powerful language vs polluting it with all
    kinds of capabilities which introduce other problems. After all, I have never had to worry about int or double boundary
    alignment when overlaying a structure on a buffer; nor have I ever gotten a segmentation violation
    (all from the world of C and its variants).

    Regards,
    Jim
    Last edited by jim829; 06-05-2016 at 09:41 PM. Reason: s/every/ever
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Why can't we override a STATIC method?

    I'm not at all sure what you'd gain (apart from the possible confusion Jos mentions).

    In any case, it is recommended that static methods are called using the classname, and not via an object reference, for just this issue. I've seen people hide a static method in a subclass and then get confused as to why (using your Animal example above) the call to eat() doesn't call the one in Cat that they wrote.

    You get a warning in most IDEs about that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Why can't we override a STATIC method?

    @Jos, Jim, Tolls Thank you very much. I got it.

    Your questions are no different than
    why doesn't Java have...
    Is there something wrong with my questions?

    When I read a rule, I think about why and what implications it would have. Previously I used to read stuff and also wrote small programs to practice it, but it hardly stayed in my mind. Now I feel I am finally learning some Java, because I am learning things with some context. I think understanding what might have motivated a design decision does help understand a language better, isn't it.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Why can't we override a STATIC method?

    Quote Originally Posted by Zarah View Post
    @Jos, Jim, Tolls Thank you very much. I got it.



    Is there something wrong with my questions?

    When I read a rule, I think about why and what implications it would have. Previously I used to read stuff and also wrote small programs to practice it, but it hardly stayed in my mind. Now I feel I am finally learning some Java, because I am learning things with some context. I think understanding what might have motivated a design decision does help understand a language better, isn't it.
    There's nothing wrong with your questions; sometimes, at least to me, they resemble the questions asked on LinkedIn by some Indian students; they ask the most trivial stuff, e.g. "why should main( ...) be static?", "why should it return 'void'?", "why does it take a String[] argument?" etc. etc. When you're up to it, try to read to read the JLS (Java Language Specification); it answers all of your questions, alhough it can be 'lawyer speak' sometimes; it isn't an easy read ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Why can't we override a STATIC method?

    Quote Originally Posted by Zarah View Post
    Is there something wrong with my questions?
    Of course not. I was just pointing out that there are many questions one could ask as to why something was done one way or another
    or not allowed at all. And there are some questions which we may never know the answers to. Many years ago, I used to participate in
    telecom standards committees (think ANSI). So for eight hours a day for a week folks would discuss various aspects of why something
    should or should not be allowed. 4 bits vs 8 bits or why was this bit placed in one location vs another. Quite often politics entered into
    the situation. One organization had to have this but no one else cared. But the rationale for those decisions never made it into the standard.
    To find out why you would need to dig up the minutes of the meetings or talk to one of the participants. I assume it is similar with the folks
    that participate in the design of a language.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Can we override static method?
    By srinivasmallabathula in forum Advanced Java
    Replies: 3
    Last Post: 06-24-2011, 02:29 PM
  2. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  3. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  4. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  5. why we cann't override static methods
    By haoberoi in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 11:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •