This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending a class with private constructor

 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to extend a class which only has a no-arg constructor that's been declared private? ie


If you cannot, I believe the class has the same effect as being declared as final. java.lang.Math has a private constructor and the class is also declared as final. Is it necessary to declare a class as final once you have a private constructor. Please throw some light into this issue. Thank you.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jacob Thomas:
Is it possible to extend a class which only has a no-arg constructor that's been declared private? ie


Yes:



If you cannot, I believe the class has the same effect as being declared as final. java.lang.Math has a private constructor and the class is also declared as final. Is it necessary to declare a class as final once you have a private constructor. Please throw some light into this issue. Thank you.


Final classes can have non-private constructors, So having only private constructors is not at all the same thing as being a final class! Some classes are designed to be "utility" classes -- a collection of static methods, and not meant to be instantiated. Yes, Math is an example of this and so is javax.swing.SwingUtilities. The convention is to give these classes a private constructor (and define no nested subclasses like I did!). At that point there is not reason to gilt the lily and declare it final as well. There is an informal anticipation that final classes are instantiable.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you make a class final - then it cannot be extended. Making a class final encourages object composition over inheritance.

If you have a private default constructor, then it cannot be instantiated directly. You need to go through other methods like getInstance(), valueOf() etc. This gives more control over how we create objects. The singleton design pattern uses a private constructor. Also, immutable objects can use private constructors to reuse objects.

So, final and private constructors are two different things. It is the best practice to use both of the above where possible.
 
Vikram Waikar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in a way u r right .. but it is not the complete truth ... as shown earlier u can have an inner class extend a class that has only private constructor.
can we have an outer class that does the same, i dont think so
any thoughts ???
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anything is possible. At runtime you could take a class's private constructor and make it accessible, then dynamically generate the byte code for top-level subclass of it. Does that sound like fun?
 
Jacob Thomas
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for you reply Jeff. You said it's also possible to extend the class using outer class. Could you possibly demonstrate this with code? Thanks
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jacob Thomas:
Thanks very much for you reply Jeff. You said it's also possible to extend the class using outer class. Could you possibly demonstrate this with code? Thanks


Sadly, not I can not because it is beyond my puny abilities. It can be done with libraries like cglib: http://sourceforge.net/projects/cglib
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

private default constructor

I think you mean private no-arg (or niladic as it is sometimes called) constructor, which is not the same thing as a default constructor (JLS 8.8.9). I know that many tertiary institutions promote the misuse of the term "default constructor", which is unfortunate and misleading.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64608
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:

(or niladic as it is sometimes called) constructor


I've also seen and used the term nullary constructor.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niladic seems obscure: http://onelook.com/?w=niladic&ls=a
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:
Niladic seems obscure: http://onelook.com/?w=niladic&ls=a


I've heard it used quite often - that is, at least I don't consider it to be obscure. I know of a few others who I assume would agree. First time for everything

On a note related to the original question: http://jqa.tmorris.net/GetQAndA.action?qids=17
 
Hendrik Reimann
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:





Hello

Can You explain this code to me. Especially the part in class Child. Is that a constructor inside Child?

Hendrik
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's actally an instance initializer. Instance initializers are executed whenever you create an instance of a class. They are used as constructors in anonymous classes.

regards,
vijay.
 
Arun Maalik
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not possible that if U declear a constructor in a main claa as private and want to extends it by other class coze private access specifier is only concern with othe class in which it has been decleared not even with subclass.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic