3

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?

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?

Community
  • 1
  • 1
wrzasa
  • 1,113
  • 10
  • 20

4 Answers4

7

Static methods are not inherited, if you want your subclass to be your application entry point, program the main method in the subclass.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
  • (This is basically to say that you can't do what you're trying to do.) – Louis Wasserman Jul 03 '12 at 14:42
  • Well... they are inherited, because I can call on the SubClass the main method derived from the SuperClass and it works. The problem is to do in the main what I need to do. – wrzasa Jul 03 '12 at 14:50
  • 2
    @WRz A child reference can directly invoke methods defined in a parent class, but that does not make the method virtual. It is just syntactic sugaring. Neither you can override a static method, you can hide the parent implementation by offering another implementation with the same signature in the child class though. With static methods, the method implementation to be invoked is chosen at compile time, based on the type of the reference you use to access it, contrary to virtual methods, whose implementation is chosen at runtime, based on the actual type of a given object. – Edwin Dalorzo Jul 03 '12 at 15:08
  • OK, that concludes the problem. Thank you for quick and exhaustive answer. A place to discuss this Java 'feature' is probably somewhere else... – wrzasa Jul 03 '12 at 17:14
1

You could use Spring IOC for example.

Create an xml file like the following and put in your classpath:

appconfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <bean id="someBean" class="com.company.SubClass"/>

</beans>

Then in your main code you could do something like this:

   public static void main(String args[]) {
       ApplicationContext context = ClassPathXmlApplicationContext("appconfig.xml");
       SuperClass startClass = (SuperClass) context.getBean("someBean");
       startClass.someMethod();
   }

Then your SuperClass will not know about its subclasses (but will know about Spring instead...).

You will have to add some Spring jar files to your classpath as well.

maba
  • 47,113
  • 10
  • 108
  • 118
0

You can not inherit static method in subclasses but if you want to make a method like virtual in c++ , make your method abstract or protected

Pooya
  • 4,385
  • 6
  • 45
  • 73
0

If by I would like to be able to run the SubClass as standalone program you mean that you want to be able to run something like java my.app.SubClass, that doesn't work, because as everyone already pointed out, static methods are not inherited.

Depending on why you want this strange subclass nesting, you would find a workaround by implementing a non-static main like this:

public class SuperClass{ 
  public static void main(String[] args) {
     SuperClass c = //figure out which class to load via a factor or something
     c.nonStaticMain(args);
  }
  protected void nonStaticMain(String[] args) {
    //do everything from your old main() here
  }
} 
Jochen
  • 2,277
  • 15
  • 22