Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting the main method

I want to define a base class that defines a main method that instantiates the class, and runs a method. There are a couple of problems though. Here is the base class:

public abstract class Strategy
{
    abstract void execute(SoccerRobot robot);

    public static void main(String args)
    {
        Strategy s = new /*Not sure what to put here*/();
        s.execute(new SoccerRobot())
    }
}

And here is an example derived class:

public class UselessStrategy
{
    void execute(SoccerRobot robot)
    {
        System.out.println("I'm useless")
    }
}

It defines a simple execute method, which should be called in a main method upon usage as a the main application. However, in order to do so, I need to instantiate the derived class from within the base class's main method. Which doesn't seem to be possible.

I'd rather not have to repeat the main method for every derived class, as it feels somewhat unnessary.

Is there a right way of doing this?

like image 918
Eric Avatar asked Jun 07 '10 09:06

Eric


People also ask

Can main method be inherited?

main method is only an static method with a special signarure. The question is: can be static methods inherited? The answer is yes.

What is inherited method?

Inheritance is the procedure in which one class inherits the attributes and methods of another class. The class whose properties and methods are inherited is known as the Parent class. And the class that inherits the properties from the parent class is the Child class.

Can a class with main method be extended?

You can have more than one class in the same file, but only one public , as Base isn't a public class, but it's not a recommended practice. @user1107888 yes, as Factorial extends Base it also inherits the main method.

Which methods can be inherited by the child class?

Types of inheritance Single Inheritance: refers to a child and parent class relationship where a class extends the another class. Multilevel inheritance: refers to a child and parent class relationship where a class extends the child class.


5 Answers

Move the main method out into a separate class. Separate concerns
Strategy (the name says it all)
Launcher (assembling components together and triggering execution)

public class Launcher
{
    public static void main(String args)
    {
       Strategy s = new UselessStrategy();
          //OR Strategy s = CreateInstance(args[0]) ;
          //OR equiv mechanism for Dependency Injection if you don't want to hardcode the derived strategy to use.
        s.execute(new SoccerRobot())
    }
}
like image 122
Gishu Avatar answered Oct 05 '22 07:10

Gishu


Static methods, such as "main", are not inherited but can be called directly. As a workaround, you could parameterize the class name as an argument to the main method:

public static void main(String args) throws Exception
{
  String className = (args.length > 0) ? args[0] : 'UselessStrategy';
  Strategy s = (Strategy) Class.forName(className).newInstance();
  s.execute(new SoccerRobot())
}

If Class.forName is not possible, then maintaining a mapping of class names can provide a lookup table, per Andreas_D's comment:

private static Map<String, Class<? extends Strategy>> STRATEGY_NAME =
    new HashMap<String, Class<? extends Strategy>>();

static {
    STRATEGY_NAME.put("Useless", UselessStrategy.class);
    STRATEGY_NAME.put("Better", BetterStrategy.class);
}

public static void main(String args[]) throws Exception {
    String className = (args.length > 0) ? args[0] : null;
    Class<? extends Strategy> klass = STRATEGY_NAME.get(className);
    if (klass == null) klass = UselessStrategy.class;
    Strategy s = klass.newInstance();
    s.execute();
}

Automated methods for maintaining the mapping could be devised, such as using reflection, if the need arises.

like image 45
maerics Avatar answered Oct 05 '22 07:10

maerics


You can define the class in a static block in the subclass.

public abstract class Strategy
{
    protected static Class<? extends Strategy> instanceClass;

    abstract void execute(SoccerRobot robot);

    public static void main(String args)
    {
        Strategy s = instanceClass.newInstance()
        s.execute(new SoccerRobot())
    }
}

and then

public class UselessStrategy extends Strategy
{
    static {
        instanceClass = UselessStrategy.class;
    }

    void execute(SoccerRobot robot)
    {
        System.out.println("I'm useless")
    }
}
like image 37
Kevin Lee Avatar answered Oct 05 '22 08:10

Kevin Lee


You cannot instantiate an abstract class, but you definitely can instantiate a derived class from the base class. So just remove abstract from class definition

public class UselessStrategy

and do

Strategy s = new UselessStrategy();
like image 39
unbeli Avatar answered Oct 05 '22 06:10

unbeli


I'd rethink this.

Put the code that you'd like executed somewhere else, preferably a non-static method, and call that. main() shouldn't be used this way.

I'd recommend creating a separate Strategy class in lieu of main.

like image 37
duffymo Avatar answered Oct 05 '22 07:10

duffymo