Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a base class be marked abstract if there are no abstract members?

Is it okay for a class to be marked as abstract if it has no abstract members? Even if there is no practical reason for to directly instantiate it? (aside from unit tests)

like image 789
kelloti Avatar asked Feb 04 '11 18:02

kelloti


2 Answers

Yes, it is reasonable and beneficial to mark explicitly as abstract a base class that should not be instantiated -- even in the absence of abstract methods.

  • It enforces the common guideline to make non-leaf classes abstract.
  • It prevents other programmers from creating instances of the class. This may make it easier for you to add abstract methods to it later.
like image 163
Andy Thomas Avatar answered Sep 30 '22 05:09

Andy Thomas


Do you want that class to ever have an actual instance? If yes, then don't mark it abstract. If no, then mark it abstract.

like image 27
Jeff Hubbard Avatar answered Sep 30 '22 05:09

Jeff Hubbard