Are static methods inherited in Java?

后端 未结 14 1365
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 05:08

I was reading A Programmer’s Guide to Java™ SCJP Certification by Khalid Mughal.

In the Inheritance chapter, it explains that

Inherit

相关标签:
14条回答
  • 2020-11-22 05:53

    We can declare static methods with same signature in subclass, but it is not considered overriding as there won’t be any run-time polymorphism.Because since all static members of a class are loaded at the time of class loading so it decide at compile time(overriding at run time) Hence the answer is ‘No’.

    0 讨论(0)
  • 2020-11-22 05:58

    If that's what the book really says, it's wrong.[1]

    The Java Language Specification #8.4.8 states:

    8.4.8 Inheritance, Overriding, and Hiding

    A class C inherits from its direct superclass all concrete methods m (both static and instance) of the superclass for which all of the following are true:

    • m is a member of the direct superclass of C.

    • m is public, protected, or declared with package access in the same package as C.

    • No method declared in C has a signature that is a subsignature (§8.4.2) of the signature of m.

    [1] It doesn't say that in my copy, 1st edition, 2000.

    0 讨论(0)
  • 2020-11-22 05:59

    B.display() works because static declaration makes the method/member to belong to the class, and not any particular class instance (aka Object). You can read more about it here.

    Another thing to note is that you cannot override a static method, you can have your sub class declare a static method with the same signature, but its behavior may be different than what you'd expect. This is probably the reason why it is not considered inherited. You can check out the problematic scenario and the explanation here.

    0 讨论(0)
  • 2020-11-22 06:00

    Static methods are inherited in Java but they don't take part in polymorphism. If we attempt to override the static methods they will just hide the superclass static methods instead of overriding them.

    0 讨论(0)
  • 2020-11-22 06:01

    Static methods in Java are inherited, but can not be overridden. If you declare the same method in a subclass, you hide the superclass method instead of overriding it. Static methods are not polymorphic. At the compile time, the static method will be statically linked.

    Example:

    public class Writer {
        public static void write() {
            System.out.println("Writing");
        }
    }
    
    public class Author extends Writer {
        public static void write() {
            System.out.println("Writing book");
        }
    }
    
    public class Programmer extends Writer {
    
        public static void write() {
            System.out.println("Writing code");
        }
    
        public static void main(String[] args) {
            Writer w = new Programmer();
            w.write();
    
            Writer secondWriter = new Author();
            secondWriter.write();
    
            Writer thirdWriter = null;
            thirdWriter.write();
    
            Author firstAuthor = new Author();
            firstAuthor.write();
        }
    }
    

    You'll get the following:

    Writing
    Writing
    Writing
    Writing book
    
    0 讨论(0)
  • 2020-11-22 06:01

    This concept is not that easy as it looks. We can access static members without inheritance, which is HasA-relation. We can access static members by extending the parent class also. That doesn't imply that it is an ISA-relation (Inheritance). Actually static members belong to the class, and static is not an access modifier. As long as the access modifiers permit to access the static members we can use them in other classes. Like if it is public then it will be accessible inside the same package and also outside the package. For private we can't use it anywhere. For default, we can use it only within the package. But for protected we have to extend the super class. So getting the static method to other class does not depend on being Static. It depends on Access modifiers. So, in my opinion, Static members can access if the access modifiers permit. Otherwise, we can use them like we use by Hasa-relation. And has a relation is not inheritance. Again we can not override the static method. If we can use other method but cant override it, then it is HasA-relation. If we can't override them it won't be inheritance.So the writer was 100% correct.

    0 讨论(0)
提交回复
热议问题