Having a main method in an abstract class

I know it is legal to have a main method in an abstract class because Eclipse allows me to do the following and run the class as a Java application. But does it make sense to do something like this?

Is there a real world scenario where you need to have a main method in an abstract class?

public abstract class Automobile
{
    public Boolean powerOn()
    {
        // generic implementation for powering on an automobile
        return true;
    }


    public void move()
    {
        // generic implementation for move
    }

    public void changeDirection(String newDir)
    {
        // generic implementation for changing direction
    }

    public abstract void accelerate(Integer changeInSpeed);
    public abstract Integer refuel(Integer inputFuel);

    public static void main(String[] args) 
    {
        System.out.println("I am a main method inside an abstract Automobile class");
    }
}

      

+3


source to share


3 answers


No, not at all. Trying to create a class hierarchy where different classes use the same main method doesn't seem to be helpful. See this example:

public abstract class A {

    public static void doStuff() {
        System.out.println("A");
    }

    public static void main(String[] args) {
        System.out.println("starting main in A");
        doStuff();
    }
}

class B extends A {
    public static void doStuff() {
        System.out.println("B");
    }
}

      

output

c:\Users\ndh>java A
starting main in A
A

      

which is expected but boring, and



c:\Users\ndh>java B
starting main in A   
A        

      

which doesn't do what you want.

Shadowing static method calls does not work like virtual overriding instance methods, you must start by explicitly naming the class used as a starting point to find the correct method signature (or by default you get the class in which the call is made). The problem is that the main method cannot know about the subclasses (or how to put it on an abstract class), so there is no good way to get the subclass-related information into the main method of the superclass.

My preference is to minimize the use of the static keyword by reserving it for constants (although not as many as enums came out) and stateless functions with no dependencies. Use object oriented methods instead. With static methods, you have to be specific. With OO, the idea is that you can avoid specifics and let the subclasses take care of themselves.

+2


source


You are asking about a scenario where a method needs to be used main

in a class abstract

, but I could ask the other way around: is there a need for a method main

in a non- abstract

class?

Obviously, its completely irrelevant whether the application's entry point is in the class abstract

or not. Important:

  • This class should be public

  • It should have a name that is useful to the outside world as it will appear in a non-Java context, i.e. command line, run scripts or XML files, documentation, etc.


However, if you have an application that consists of one base class public abstract

with a short name (for example Automobile

) and some implementation classes that are not public

and / or have hard-to-remember names (for example AutomobileXF838EngineImpl

), there is a point in choosing this base class for placing the entry point of the application.

But this only applies to fairly small applications where the number of classes matters. For larger applications, you will usually have a dedicated class to serve as the application entry point where the method resides main

. There may even be multiple startup classes in different environments or environments.

Thus, these initial classes are not a base class abstract

or an implementation class of such a base class, but are completely unrelated to such class hierarchies. Since they are not the base classes of the type hierarchy, they usually are not abstract

.

+3


source


you can create objects of another class and use other class methods by composition

class Test1 {

    int x = 10;

    public void display() {
        System.out.println("Hello! This is Test1 class");
    }
}

public abstract class Test {

    public static void main(String args[]) {
        Test1 t1 = new Test1();
        System.out.println("From abstract class main(): " + t1.x);
        t1.display();
    }
}

      

0


source







All Articles