This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Why can’t static methods be abstracted in Java?

The question is why can’t abstract static methods be defined in Java? For example,

abstract class foo { abstract void bar( ); // <– this is ok abstract static void bar2(); //<– this isn’t why? }

Answer:


A lot of knowledge points, really need to write out will master ! ! !   \color{purple} a lot of knowledge points, really need to write out just can master!! {~}

An annotation for the abstract method indicates that the method must be overridden in a subclass.

In Java, static members (methods or fields) cannot be overridden by subclasses (this is not necessarily true in other object-oriented languages, see SmallTalk). Static members may be hidden, but are fundamentally different from overrided.

Because static members cannot be overridden in subclasses, abstract annotations cannot be applied to them.

By the way – other languages also support static inheritance, just like instance inheritance. Syntactically, these languages typically require that the class name be included in the statement. For example, in Java, assuming you are writing code in ClassA, they are equivalent statements (if methodA () is a static method and there is no instance method with the same signature) :

ClassA.methodA();

and

methodA();

In SmallTalk, the class name is not optional, so the syntax is (note that SmallTalk does not use it. To separate “subject” from “verb” and instead use it as the statemend terminator) :

ClassA methodA.

Because the class name is always needed, you can always determine the correct “version” of a method by traversing the class hierarchy. I occasionally miss out on static inheritance for its value, and suffered from its lack when I first started using Java. Also, SmallTalk is duck type (and therefore does not support programming by contract). Therefore, it has no abstract modifier for a class member.

Answer:

Because “abstract” means: “does not implement any function”, and “static” means: “has function even without object instance”. This is a logical contradiction.

The article translated from kgs4h5t57thfb6iyuz6dqtun5y ac4c6men2g7xr2a – stackoverflow – com. Translate. Goog/questions / 3…

The authors suggest that while abstractions may need to be implemented, statics themselves need not be implemented


Welcome to my column S t a c k O v e r F l o w . I select the best questions and answers and test them frequently in interviews ! ! !   \color{red} Welcome to my column StackOverFlow, I will filter the quality of the interview test!! {~}


There are the latest and elegant ways to do this, and I will write my thoughts on this q&A at the end of the article \color{red} has the latest, elegant implementation, and I will also write my opinion on this question at the end of the article {~}

Thank you for reading this, if this article is well written and if you feel there is something to it

Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!

If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️