Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending the main class in Java

Tags:

java

eclipse

I need to extend the functionality of my main class, by overriding some of its methods. I was expecting that the class extending the main class would be able to be run. However, Eclipse doesn't recognize MyLauncher as a runnable class. In the following code, I have a setup() method that is overridden by the subclass. What I want is a way to run the main(..) from the super class but also the setup from the subclass.

// Launcher.java
public class Launcher {

    Launcher instance;

    public static void main (args[]) {
        instance = new Launcher(); // This is likely the problem
        instance.setup();
    }

    public void setup() {
        System.out.println("Default agent setup.");
    }
}

// MyLauncher.java
public class MyLauncher extends Launcher {

    public void setup() {
        System.out.println("New agent setup!");
    }
}

I accept alternatives to this. I can't add a main method to the subclass, though. The Launcher class is inside an API i'm making, so it can't refer to the class MyLauncher that is using the API.

edit: I think this is to specific to my problem. I decided to search for a new approach. Since I'm working with JDT, I'm going to parse the Launcher and inject the class.

like image 226
ecc Avatar asked May 14 '14 13:05

ecc


People also ask

Can we extend main class in Java?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another.

Can we extend main method class?

There can only be one main method in your project, that is one entry point to the program. So let's assume you're going to be keeping the main method in the Launcher class. And unless you want the setup() method from the launcher to be called you'd want to do: instance = new MyLauncher();

What are extension classes in Java?

Extensions are groups of packages and classes that augment the Java platform through the extension mechanism. The extension mechanism enables the runtime environment to find and load extension classes without the extension classes having to be named on the class path.

When should you extend a class?

You extend a class when you want the new class to have all the same features of the original, and something more. The child class may then either add new functionalities, or override some funcionalities of the parent class.


4 Answers

Static methods are not inherited, they're always bound to the class that defines them, and need to be called explicitely.

In you case, the MyLauncher needs a main() method too, and could then delegate to the main() method of Launcher:

public class MyLauncher extends Launcher {

    public static void main (String[] args) {
        Launcher.main(args);
    }

    protected void setup() {
        System.out.println("New agent setup!");
    }
}
like image 82
Peter Walser Avatar answered Sep 27 '22 20:09

Peter Walser


Protected methods can not be called from outside. So the MyLauncher.setup() do not override Launcher.setup() and instance.setup(); calls the public method from Class Launcher.

like image 36
Grim Avatar answered Sep 27 '22 20:09

Grim


There can only be one main method in your project, that is one entry point to the program. So let's assume you're going to be keeping the main method in the Launcher class.

Your main method signature should be:

public static void main (String args[])

And unless you want the setup() method from the launcher to be called you'd want to do:

instance = new MyLauncher();

That would call the setup() method from MyLauncher.

If you want to call setup() from the Launcher class you need to instantiate the launcher class:

instance = new Launcher();
like image 44
Eidan Spiegel Avatar answered Sep 27 '22 19:09

Eidan Spiegel


If you want to be able to run MyLauncher.setup(), the variable must be a MyLauncher. You are initializing and storing a Launcher in the main() function.

If the two classes are in the same package, or Launcher.java imports the MyLauncher class, then the main() function in Launcher should be able to be:

public class Launcher {
    Launcher instance;

    public static void main(String[] args) {
        instance = new MyLauncher();
        if(instance instanceof MyLauncher) {
            ((MyLauncher) instance).setup();
        } else
        {
            instance.setup();
        }
    }
}
like image 28
Tripp Kinetics Avatar answered Sep 27 '22 19:09

Tripp Kinetics