Why do we say that a static method in Java is not a virtual method?

前端 未结 5 1749
北恋
北恋 2021-01-05 06:58

In object-oriented paradigm, a virtual function or virtual method is a function or method whose behavior can be overridden within an inheriting class by a f

相关标签:
5条回答
  • 2021-01-05 07:14

    It's simple, a static method cannot be overridden by an inheriting class, since it's not inherited. So it's not virtual.

    What you call "overriding a static method" is actually only defining another static method on another class. It'll only "hide" (and that's actually a much stronger word than what'd be actually true there) the other one, not override it.

    0 讨论(0)
  • 2021-01-05 07:14

    An abstract class in Java is nothing but the pure virtual method equivalent to C++.

    A class is not a method. An abstract class doesn't have to have "virtual" or abstract methods, or even any methods.

    Something C++ developers put down Java features as just like C++ renamed without understanding the differences. ;)

    Why do we say that a static method in Java is not a virtual method?

    Not sure who says this, but static methods are not polymorphic.

    Even if we can override the static method

    We can't, you can only hide or overload a static method.

    Whether you use the class or sub-class or instance to invoke a static method the actual class or instance is ignored. e.g. you can do

    ((Thread) null).yield();
    
    0 讨论(0)
  • 2021-01-05 07:27

    Suppose you have class A

    public class A
    {
        public static void doAStaticThing()
        {
            System.out.println("In class A");
        }
    }
    

    And B

    public class B extends A
    {
        public static void doAStaticThing()
        {
            System.out.println("In class B");
        }
    }
    

    And a method in another class like this:

    public void foo()
    {
        B aB = new B();
        bar(B);
    }
    
    public void bar(A anA)
    {
        anA.doAStaticThing(); // gives a warning in Eclipse
    }
    

    The message you will see on the console is

    In class A
    

    The compiler has looked at the declared type of anA in method bar and statically bound to class A's implementation of doAStaticThing(). The method is not virtual.

    0 讨论(0)
  • 2021-01-05 07:32

    Because polymorphism applies to objects, while a static method doesn't relate to any object (but to a class).

    0 讨论(0)
  • 2021-01-05 07:33

    You can't override static methods. They are bound at compile-time. They are not polymorphic. Even if you try to invoke it as if it were an instance method (which IMO you shouldn't do) it's bound to the compile-time type of that expression, and the execution-time value is completely ignored (even if it's null):

    Thread otherThread = null;
    otherThread.sleep(1000); // No errors, equivalent to Thread.sleep(1000);
    

    This behaviour can be very confusing for a reader, which is why at least some IDEs allow you to generate warnings or errors for accessing static members "through" a reference. It was a flaw in Java's design, pure and simple - but it doesn't make static methods virtual at all.

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