Is it possible to create the main method in an abstract class?

0

Can I use the main method in an abstract class?

An abstract class can not be instantiated, this I know. It can be referenced, yes.

But can I use the main method in it?

I'm not talking about "compile or not compile" I'm talking about a good programming practice.

Can I or should not I?

Example:

public abstract class funcionario{
    public static void main(String[] args){
        Funcionario f1 = new Diretor();
    }
}
    
asked by anonymous 14.02.2018 / 18:04

1 answer

6

You can do this, but it does not usually make much sense in practice.

A method, even though it is main , ideally has a connection with the class where it is. This is directly related to the single responsibility principle , with principle of high cohesion and with low coupling principle a>.

So, the ideal place to put main depends heavily on the architecture and structure of your program. If there is a class that of course already represents your entire program being executed, it makes sense that main is there to maintain high cohesion and low coupling, even if this class is abstract . However, it would hardly make sense for an abstract class to represent your program as a whole, so that would hardly make sense . Also, if the class representing your program as a whole is an abstract class, it would probably be doing too much, violating the principle of single responsibility.

In cases where there is no class representing the program as a whole, you would put the main method in a class just for it. The sole responsibility of this class would be to be the entry point of the program. Naturally, this class would not make sense at all.

In fact, the concept of abstract class itself is something that is being questioned as a result of the concept of implementation inheritance being questioned. Inheritance introduces a strong subclass coupling to the superclass and is something that can almost always be eliminated with refactorings and replaced with composition. Type inheritance is handled with interfaces. As of Java 8, you can have default and static implementations on interfaces, which further reduces the need to use inheritance and thus the need to use abstract classes by providing implementation inheritance through interfaces as well.

In summary, if you are concerned with good programming practices, you may end up with a project where there will be no abstract classes (or very few), so there would be none where you would place% p>

In your specific case, it looks like you have main . It turns out it would be best if Diretor extends funcionario had a method Funcionario and then you would have public Cargo getCargo() . This would leave the structure of your project more organized and eliminate inheritance and with it the need to have abstract classes.

Furthermore, the fact that public class Diretor implements Cargo is in the main class demonstrates that there has been a violation of the principle of single liability in that class. The fact that funcionario is likely to use many things other than employee (especially when coupling the superclass in the subclass instead of the reverse) also demonstrates that coupling is high rather than low and cohesion is low rather than high.

So your question is a bit a XY problem . The main method is in an abstract class is a bad smell , but actually "the hole is lower."

    
14.02.2018 / 18:35