Why abstract method cannot be static
Hope they solve those limitations in a next release. You can't override a static method, so making it abstract would be meaningless. Moreover, a static method in an abstract class would belong to that class, and not the overriding class, so couldn't be used anyway. The abstract annotation to a method indicates that the method MUST be overriden in a subclass. In Java, a static member method or field cannot be overridden by subclasses this is not necessarily true in other object oriented languages, see SmallTalk.
A static member may be hidden , but that is fundamentally different than overridden. Since static members cannot be overriden in a subclass, the abstract annotation cannot be applied to them. As an aside - other languages do support static inheritance, just like instance inheritance. From a syntax perspective, those languages usually require the class name to be included in the statement. For example, in Java, assuming you are writing code in ClassA, these are equivalent statements if methodA is a static method, and there is no instance method with the same signature :.
In SmallTalk, the class name is not optional, so the syntax is note that SmallTalk does not use the. Because the class name is always required, the correct "version" of the method can always be determined by traversing the class hierarchy.
For what it's worth, I do occasionally miss static inheritance, and was bitten by the lack of static inheritance in Java when I first started with it. Additionally, SmallTalk is duck-typed and thus doesn't support program-by-contract. Thus, it has no abstract modifier for class members. 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.
This would allow you to invoke your methods statically , yet be able to alter the implementation say for a Test environment. Theoretically, you could do this on a ThreadLocal as well, and be able to set instance per Thread context instead rather than fully global as seen here, one would then be able to do Request.
Real world usually do not require the ThreadLocal approach and usually it is enough to be able to alter implementation for Test environment globally. 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. 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.
A static method, by definition, doesn't need to know this. Thus, it cannot be a virtual method that is overloaded according to dynamic subclass information available through this ; instead, a static method overload is solely based on info available at compile time this means: once you refer a static method of superclass, you call namely the superclass method, but never a subclass method.
According to this, abstract static methods would be quite useless because you will never have its reference substituted by some defined body. I see that there are a god-zillion answers already but I don't see any practical solutions.
Of course this is a real problem and there is no good reason for excluding this syntax in Java. Since the original question lacks a context where this may be need, I provide both a context and a solution:. Suppose you have a static method in a bunch of classes that are identical. These methods call a static method that is class specific:. There may be a lot of these calsses: C3 C4 etc.
If static abstract was allowed, you'd eliminate the duplicate code by doing something like:. However, this can be circumvented with static class construct, which is allowed:. Assume there are two classes, Parent and Child. Parent is abstract. The declarations are as follows:. This means that any instance of Parent must specify how run is executed. 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. Just the class. Static methods belongs to class and not object. Methods in abstract class are dynamically binded to their functionality. Declaring a method as static means we can call that method by its class name and if that class is abstract as well, it makes no sense to call it as it does not contain any body, and hence we cannot declare a method both as static and abstract.
As abstract methods belong to the class and cannot be overridden by the implementing class. Even if there is a static method with same signature , it hides the method ,does not override it. So it is immaterial to declare the abstract method as static as it will never get the body. Thus, compile time error. A static method can be called without an instance of the class. In your example you can call foo. Following code would work:. 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. If bar2 now has no implementation that's what abstract means , you can call a method without implementation. That's very harmful. I believe I have found the answer to this question, in the form of why an interface's methods which work like abstract methods in a parent class can't be static. Here is the full answer not mine.
Basically static methods can be bound at compile time, since to call them you need to specify a class. This is different than instance methods, for which the class of the reference from which you're calling the method may be unknown at compile time thus which code block is called can only be determined at runtime.
If you're calling a static method, you already know the class where it's implemented, or any direct subclasses of it.
If you define. Then any Foo. With this in mind, the only purpose of a static abstract method would be to enforce subclasses to implement such a method. Keep in mind that due to type erasure generics only exist at compile time.
So, would it be useful? Yes, and maybe that is why Java 8 is allowing static methods in interfaces though only with a default implementation. Why not abstract static methods with a default implementation in classes? Simply because an abstract method with a default implementation is actually a concrete method. Apparently, merely because of the way Java identifies which code block it has to execute first part of my answer. Now the thing is we can declare static complete methods in interface and we can execute interface by declaring main method inside an interface.
Because abstract mehods always need implementation by subclass. But if you make any method to static then overriding is not possible for this method. The idea of having an abstract static method would be that you can't use that particular abstract class directly for that method, but only the first derivative would be allowed to implement that static method or for generics: the actual class of the generic you use.
That way, you could create for example a sortableObject abstract class or even interface with auto- abstract static methods, which defines the parameters of sort options:. Now you can define a sortable object that can be sorted by the main types which are the same for all these objects:.
Note that the instance of SortableList can directly access the static method of "T":. The problem with having to use an instance is that the SortableList may not have items yet, but already need to provide the preferred sorting. First, a key point about abstract classes - An abstract class cannot be instantiated see wiki. So, you can't create any instance of an abstract class. Now, the way java deals with static methods is by sharing the method with all the instances of that class.
So, If you can't instantiate a class, that class can't have abstract static methods since an abstract method begs to be extended. As per Java doc :. A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods. In Java 8, along with default methods static methods are also allowed in an interface. This makes it easier for us to organize helper methods in our libraries.
We can keep static methods specific to an interface in the same interface rather than in a separate class. Another example of using static methods is also given in doc itself:. Because 'abstract' means the method is meant to be overridden and one can't override 'static' methods. Regular methods can be abstract when they are meant to be overridden by subclasses and provided with functionality. Hence our assumption for static func method to be abstract fails. Therefore, a static method cannot be abstract.
Then that method will be coded as: ,The above code is incorrect as static methods cannot be abstract. When run, the Compilation Error that occurs is:Compilation Error: ,Scenario 1: When a method is described as abstract by using the abstract type modifier, it becomes responsibility of the subclass to implement it because they have no specified implementation in the super-class. Thus, a subclass must override them to provide method definition. Well, Java doesn't allow this because of a basic contradiction.
Why can't static methods be abstract in Java? Asked ago. Active 3 hr before. Viewed times. Other "static-undefined" queries related to "Why can't static methods be abstract in Java? Example: Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
When run, the Compilation Error that occurs is: Compilation Error: prog. Assuming we make a static method abstract. Then that method will be written as:. Skip to content. Change Language. Related Articles. Table of Contents. Save Article.
0コメント