java - Having main method in an abstract class -


i know it's legal have main method in abstract class, because eclipse allows me following , run class java application. make sense this?

is there real world scenario 1 need have main method in abstract class?

public abstract class automobile {     public boolean poweron()     {         // generic implementation powering on automobile         return true;     }       public void move()     {         // generic implementation move     }      public void changedirection(string newdir)     {         // generic implementation changing direction     }      public abstract void accelerate(integer changeinspeed);     public abstract integer refuel(integer inputfuel);      public static void main(string[] args)      {         system.out.println("i main method inside abstract automobile class");     } } 

no, not really. trying create class hierarchy different classes share same main method doesn't seem useful. see example:

public abstract class {      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 {     public static void dostuff() {         system.out.println("b");     } } 

this prints out

c:\users\ndh>java starting main in a 

which expected boring, and

c:\users\ndh>java b starting main in            

which doesn't want.

shadowing static method calls doesn't work virtual overriding of instance methods, have start explicitly naming class use starting point finding right method signature (or default class call made). problem main method can't know subclasses (or what's point of putting on abstract class), there's no way subclass-specific information superclass main method.

my preference minimize use of static keyword, reserving constants (although not since enums came out) , stateless functions no dependencies. favor object-oriented techniques instead. static methods have specific. oo idea can avoid being specific , let subclasses take care of themselves.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -