Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Meaning of private constructors, final methods  RSS feed

 
Shyam Prasad Murarka
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Readers,
Please could anyone explain the meaning of private contructors and final methods to me. What are their scopes and limitations. Under what conditions are they used? If possible could you please provide a tiny example.
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I remember correctly (don't actually use either of these much myself), a private constructor means an instance of the object can only be created by itself. In other words you couldn't create an instance of Object1 from Object2, only from inside Object1. While I'm sure there's a use for this I've never come across it in my job.

As for final methods, I believe that's just a case where you don't want the method to be overridden. For example maybe you have a class that does a calculation on some financial numbers and you don't want anyone overridding your method in a subclass you could declare that method final.

I'm just coming up with this from memory as I have said in my job I've never actually used either. But that could be just because of the specific project I work on. Someone else might be able to correct me or even give a real world example.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a private constructor means that an object of that type can only be instantiated by code within that class. Private constructors are sometimes used to enforce singletons...





In this example the ONLY way (unless you use some reflection magic to bypass the access constraints) to get a reference to a Singleton object is via the getInstance() method.

As for final methods, it means that they cannot be overridden, as Hentay pointed out.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private constructors are used in libraries which contain only static methods, and where instantiating the class is therefore undesirable.

They also have uses in classes where only once instance of the class is ever returned, for example via a getInstance() method. Again, the private constructor prevents external instantiation. This is the Singleton design pattern I think.

Providing a private constructor prevents the compiler creating the default empty constructor if no constructor is specified.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that people have explained private constructors well enough, so here are a few words about final methods.

A subclass can inherit a final method but cannot override it. However, if a class is declared final, then its methods are also implicitly final because that class cannot be subclassed.

If a method is private, then it is implicitly final as it will not be inherited.
 
Ryan McGuire
Ranch Hand
Posts: 1142
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Carman:

In this example the ONLY way (unless you use some reflection magic to bypass the access constraints) to get a reference to a Singleton object is via the getInstance() method.


Even using reflection magic won't help. If you try to instantiate an instance of a class that has only private constructors (i.e. call Class.forName("Singleton").newInstance()) from outside that class, you will get an IllegalAccessException.

I'm not sure of all the if's and's or but's here, but that's my understanding.

Ryan
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Doesn't that "bake your noodle"?
[ May 06, 2005: Message edited by: James Carman ]
 
Ryan McGuire
Ranch Hand
Posts: 1142
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Carman:

Doesn't that "bake your noodle"?


Ok, that does indeed count as "reflection magic."
[ May 06, 2005: Message edited by: Ryan McGuire ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!