• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

purpose of private constructor in abstract class

 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me is their a practical use for having a private constructor in an abstract class??

TIA
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am mistaken about this, someone please correct me, but here is my understanding.

If there are multiple constructors defined within the abstract class, you could define some of those constructors to be private to ensure that they are only accessed from within the class itself. Perhaps this is some code that is common to all constructors, but you don't want it to be directly accessible from outside classes - you'd rather invoke that/those constructors from your other, more accessible, constructors.

However, if you only define private constructors, you're going to get a compiler error. The abstract class itself will compile without problem (I think), but any class that tries to extend that class will end up with an error. The reason for this is that the subclass needs to invoke a constructor of the parent class in order to complete the instantiation procedure. If the parent class has ONLY private constructors, you'll get an error here. This goes for ALL classes, not just abstract ones. A class with only private constructors defined is, in effect, implicitly final.
 
Swamy Nathan
Ranch Hand
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont think there is any scope for correction.

I guess we all also know that if a class is a util class like java.lang.Math which has only static methods to prevent annyone from unnecessarily instantiating such classes one can make their no arg constructor private.[Note this is not about abstract class.]

If u make a class A abstract it implies you will be extending that class A. Otherwise the abstract class A would be useless. And if you extend the abstract class A to derived class B and A had only private constructors how will any constructor of B ever invoke super()? Therefore....
[ June 17, 2004: Message edited by: Swamy Nathan ]
 
Sudhakar Krishnamurthy
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
makes sense having non-private constructor along with private constructor.
thanks
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, if you only ever want a constructor with certain arguments to be used, you could make the no-argument constructor private. This would ensure that the initialisation in the constructor with arguments is always performed and you won't have to rely on the goodwill of people extending your class.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic