What is the use of main method in abstract class?

后端 未结 4 833
攒了一身酷
攒了一身酷 2021-02-05 10:00

I know that we can write main method in abstract class, but what we can achieve from it ?

 public abstract class Sample
 {
         public static void main(Strin         


        
4条回答
  •  离开以前
    2021-02-05 10:16

    Abstract just means you can't instantiate the class directly.

    Loading a class is not the same as creating an instance of the class. And there's no need to create an instance of the class to call main(), because it's static. So there's no problem.

    Abstract just means you can't instantiate the class directly. You can have constructors if you want - they might be needed for subclasses to initiate the object state. You can have static methods, including main() and they don't need an object so calling them is fine.

    So you only got error when you try to create the object, which is when you run into the abstract limitation.

提交回复
热议问题