4

Would using a main for an abstract class be bad practice in general or is it permissible due to the nature of Abstract classes being able to have a body?

sgript
  • 317
  • 1
  • 2
  • 10

2 Answers2

5

Sure, an abstract class can have a main method, just as any class can have a main, and in fact it is one way of testing the abstract class -- if you create a concrete implementation of it within the main method. The only thing that you cannot do with an abstract class is to construct them as is, without extending them and implementing all abstract methods.

public abstract class Foo {
    public abstract void bar();

    public static void main(String[] args) {
        // anonymous inner class representation
        Foo foo = new Foo() {
            // must implement all abstract methods
            public void bar() {
                System.out.println("bar");
            }
        };
        foo.bar();
    }
}

Edit: good point by VitalyGreck:

abstract classes are abstract because they don't implement some methods in their body. having bar() implemented inside the main() method (even static) confuses users of your class. Good practice is to make two separate classes, one of them - abstract, another - with implementation and static method enclosed. Or dynamically find the enclosing class (see stackoverflow.com/questions/936684/…) using reflection.

In other words -- just because it can be done, doesn't mean that it should be done.

Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    BTW since Java8 we can have `main` method even in interfaces. – Pshemo Apr 07 '15 at 21:26
  • 4
    @Pshemo: true and which bothers me to no end. – Hovercraft Full Of Eels Apr 07 '15 at 21:27
  • But for example if that main was not directly related to the abstract class, such as for testing it, would it be better to have the main in a separate test class for the overall program? Sorry for being repetitive, I'd just like to see if it really matters as I had the impression splitting them up would be better – sgript Apr 07 '15 at 21:28
  • 1
    @sphinxyz: Realize that your project may have *many* main methods scattered about, some for testing purposes, but the **main** main method is probably best off in its own class all by itself, and not in a class that has any other function or purpose. – Hovercraft Full Of Eels Apr 07 '15 at 21:30
  • Thank you, you've been a lot of help and very responsive! :D (Will accept as solution when enabled) – sgript Apr 07 '15 at 21:31
  • 1
    Did you mean `public abstract class Foo {`? – MT0 Apr 07 '15 at 21:38
  • @MT0: indeed I did. sigh, thanks for the sharp eyes! – Hovercraft Full Of Eels Apr 07 '15 at 21:39
  • It's not what is supposed to do. You override method bar inside the static method, e.g. making an implicit class Foo$1 with bar() method implementation. Bad programming practice and bad idea. – Vitaly Greck Apr 07 '15 at 22:13
  • 1
    abstract classes are abstract because they don't implement some methods in their body. having bar() implemented inside the main() method (even static) confuses users of your class. Good practice is to make two separate classes, one of them - abstract, another - with implementation and static method enclosed. Or dynamically find the enclosing class (see http://stackoverflow.com/questions/936684/getting-the-class-name-from-a-static-method-in-java) using reflection. – Vitaly Greck Apr 07 '15 at 22:39
  • 1
    @VitalyGreck: I'll buy that and your down vote since you've given a good explanation that will help me and others. A sincere thank you. I hope that you don't mind my including your explanation as an edit to the answer. /Pete – Hovercraft Full Of Eels Apr 07 '15 at 22:42
2

You have no problemas having a main in your abstract class.

Static methods does not override, any subclass can also have the same static method.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
  • 1
    I know that it doesn't, but simply was looking to discuss whether it would be a "bad practice", thanks for your time though! – sgript Apr 07 '15 at 21:34
  • "static methods does not inherit." they are, you can invoke static method using name of its subtype (`MySubclass.staticMethodFromParent()`). One condition is that you are inheriting them from class (even abstract), but not from interface. Maybe you meant that we can't override static methods (which is true, since we are not really overriding but hiding them). – Pshemo Apr 07 '15 at 21:49
  • Hmm... you can call the method from subclasses, I wrote it wrong then – Marcos Vasconcelos Apr 07 '15 at 22:00
  • But still, this is not inheritance, this is simply importing. – Marcos Vasconcelos Apr 07 '15 at 22:01
  • 1
    I kind of agree with this point of view about inheriting static methods: http://stackoverflow.com/questions/10291949/are-static-methods-inherited-in-java – Pshemo Apr 07 '15 at 22:04