Starting with Python 3.3, it is possible to combine @staticmethod and @abstractmethod, so none of the other suggestions are necessary anymore:
@staticmethod
@abstractmethod
def my_abstract_staticmethod(...):
@abstractstaticmethod has been deprecated since version 3.3 (but is still there in Python 3.14).
Starting with Python 3.3, it is possible to combine @staticmethod and @abstractmethod, so none of the other suggestions are necessary anymore:
@staticmethod
@abstractmethod
def my_abstract_staticmethod(...):
@abstractstaticmethod has been deprecated since version 3.3 (but is still there in Python 3.14).
class abstractstatic(staticmethod):
__slots__ = ()
def __init__(self, function):
super(abstractstatic, self).__init__(function)
function.__isabstractmethod__ = True
__isabstractmethod__ = True
class A(object):
__metaclass__ = abc.ABCMeta
@abstractstatic
def test():
print 5
Videos
I created an abstract method that uses no instance data in any of its implementations, so I wanted to make it static to avoid the need to instantiate the subclass and help me to reuse the code elsewhere.
boolean canSeeSquareFrom(int x, int y, int initX, int initY) {
return 2 == Math.abs((x - initX) * (y - initY));
}Then I found out static methods can't be overridden.
I have to override the method to use polymorphism when I'm calling the method on objects, so what do I do? Does Java support a way to call a non-static method without creating unnecessary instances?
(i have a workaround I don't like, which is creating an instance of the subclass every time I need the method; is this the best we can do?)
Knight.getKnight().canSeeSquareFrom(0, 0, 2, 1);
Edit: regarding the automod, I also looked up all of the non-access modifiers and didn't see any that were helpful from what I could tell. (I was hoping I misunderstood something there)
Game.java:101: error: non-static method canSeeSquareFrom(int,int,int,int) cannot be referenced from a static context
Knight/*.getKnight()*/.canSeeSquareFrom(0, 0, 2, 1);
^On the other hand, if you absolutely needed to do this without an object instance, it's possible to do with classmethods rather than staticmethods.
from abc import ABC, abstractmethod
class MyAbstractClass(ABC):
@staticmethod
@abstractmethod
def foo(label: str):
raise NotImplementedError()
@classmethod
def foo_agnostic(cls, label: str):
"""
NOTE: Here, this method doesn't have a reference to an instance of the class.
Instead, it only has a reference to the class itself; but that is enough
to call the abstract static foo() method.
"""
cls.foo(label)
class MyDerivedClass(MyAbstractClass):
@staticmethod
def foo(label: str):
print(label)
if __name__ == "__main__":
instance = MyDerivedClass()
instance.foo("Test 1") # Outputs "Test 1"
instance.foo_agnostic("Test 2") # Outputs "Test 2"
MyDerivedClass.foo_agnostic("Test 3") # Outputs "Test 3"
... in base class I heed to have a common method which will use a particular imported derived class' static method
If I understand your question correctly, I'd say that this functionality is available out of the box with one small exception: Don't use a static method; just use a regular instance method.
Defining an abstract method in the base class will ensure that derived classes contain an implementation for that method. And, out of the box, the method defined in the derived class will get called when you call derived_object.bar().