I have a generic, abstract class (SuperClass
). I want to have there a main
method, that would be a default main for each subclass and would do the same, but with proper subclass object that derived and called it.
Like this:
public abstract class SuperClass {
// some code here...
public static void main(String args[]) {
// here instantiate the subclass
// extending this SuperClass, and call
// some methods
}
}
public SubClass extends SuperClass {
// here just implement some
// abstract methods from SupeClass
// and NOT implement main()
}
And now I would like to be able to run the SubClass
as standalone program, that executes the default main
derived from the SuperClass
. How to instantiate the proper SubClass
object in the main
method?
- I cannot do just new because in
SuperClass
I don't know the actual name of aSubClass
- I cannot do it using reflection, because I cannot get the name of the
SubClass
from a static method implemented in theSuperClass
(Getting the name of a sub-class from within a super-class)
In C++, AFAIR, there is something like virtual
modifier for a method, that I guess would be useful here. How to do in in Java?