I want to enforce a Class User to implement a static member from an abstract class Base. Is this possible, or am I using the abstract inheritance paradigm incorrectly?
Without inheritance, the User can simply implement its own static member without overriding the Base, but I would like my linter to throw a warning when a Class does not implement some static member.
What works (without enforcing the interface)
We can implicitly use the same interface in Base and User.
abstract class Base {
// This member could simply be removed and the snippet would still compile.
static final String routeName = '/base';
}
class User extends Base {
static final String routeName = '/base/user';
}
If we were to create a new Class Person that also extends Base, we have no specific implementation requirements. Ideally, I want to enforce Person to have a static member routeName.
class Person extends Base {
// No warning about a missing static member 'routeName'.
}
Example of the idea (does not compile)
The Base would contain an unimplemented static member (routeName).
This snippet does not compile unless we specify the routeName (e.g. routeName = '/base').
abstract class Base {
// IDE Error: The final variable 'routeName' must be initialized.
static final String routeName;
}
The User that implements Base would override or implement the static member.
The snippet reports a warning, unless we remove the wrongly placed @override.
class User extends Base {
// IDE Warning: Field doesn't override an inherited getter or setter.
@override
static final String routeName = '/base/user';
}
I want the IDE to throw an error for the Person Class since it does not implement the static member routeName.
class Person extends Base {
// Expected IDE Error: The static final variable 'routeName' must be initialized.
}
To me, this looks similar to how Java does it, but I am unfamiliar with the Java specifics.