Monday, 18 April 2016

Use cases for making private constructor ?


Possible use cases to make private constructor : 

  1. To implement design patterns like, Singleton or Factory
  2. To avoid making unnecessary instances , like Utility classes where all methods are static
  3. To restrict object creation of inner class till its outer class only. Not outside that
  4. To avoid inheriting the class by other 
      (Although, better option is to make class final)

No comments:

Post a Comment

Note: only a member of this blog may post a comment.