Why can't static methods be abstract in Java?

后端 未结 25 2276
不思量自难忘°
不思量自难忘° 2020-11-22 08:34

The question is in Java why can\'t I define an abstract static method? for example

abstract class foo {
    abstract void bar( ); // <-- this is ok
    ab         


        
相关标签:
25条回答
  • 2020-11-22 08:44
    • An abstract method is defined only so that it can be overridden in a subclass. However, static methods can not be overridden. Therefore, it is a compile-time error to have an abstract, static method.

      Now the next question is why static methods can not be overridden??

    • It's because static methods belongs to a particular class and not to its instance. If you try to override a static method you will not get any compilation or runtime error but compiler would just hide the static method of superclass.

    0 讨论(0)
  • 2020-11-22 08:45

    Because abstract class is an OOPS concept and static members are not the part of OOPS....
    Now the thing is we can declare static complete methods in interface and we can execute interface by declaring main method inside an interface

    interface Demo 
    {
      public static void main(String [] args) {
         System.out.println("I am from interface");
      }
    }
    
    0 讨论(0)
  • Because abstract is a keyword which is applied over Abstract methods do not specify a body. And If we talk about static keyword it belongs to class area.

    0 讨论(0)
  • 2020-11-22 08:46

    A static method can be called without an instance of the class. In your example you can call foo.bar2(), but not foo.bar(), because for bar you need an instance. Following code would work:

    foo var = new ImplementsFoo();
    var.bar();
    

    If you call a static method, it will be executed always the same code. In the above example, even if you redefine bar2 in ImplementsFoo, a call to var.bar2() would execute foo.bar2().

    If bar2 now has no implementation (that's what abstract means), you can call a method without implementation. That's very harmful.

    0 讨论(0)
  • 2020-11-22 08:48

    I also asked the same question , here is why

    Since Abstract class says, it will not give implementation and allow subclass to give it

    so Subclass has to override the methods of Superclass ,

    RULE NO 1 - A static method cannot be overridden

    Because static members and methods are compile time elements , that is why Overloading(Compile time Polymorphism) of static methods are allowed rather then Overriding (Runtime Polymorphism)

    So , they cant be Abstract .

    There is no thing like abstract static <--- Not allowed in Java Universe

    0 讨论(0)
  • 2020-11-22 08:49

    Assume there are two classes, Parent and Child. Parent is abstract. The declarations are as follows:

    abstract class Parent {
        abstract void run();
    }
    
    class Child extends Parent {
        void run() {}
    }
    

    This means that any instance of Parent must specify how run() is executed.

    However, assume now that Parent is not abstract.

    class Parent {
        static void run() {}
    }
    

    This means that Parent.run() will execute the static method.

    The definition of an abstract method is "A method that is declared but not implemented", which means it doesn't return anything itself.

    The definition of a static method is "A method that returns the same value for the same parameters regardless of the instance on which it is called".

    An abstract method's return value will change as the instance changes. A static method will not. A static abstract method is pretty much a method where the return value is constant, but does not return anything. This is a logical contradiction.

    Also, there is really not much of a reason for a static abstract method.

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