This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Extending a class with private constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extending a class with private constructor" Watch "Extending a class with private constructor" New topic
Author

Extending a class with private constructor

Jacob Thomas
Ranch Hand

Joined: May 06, 2003
Posts: 51
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

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
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.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Vikram Waikar
Greenhorn

Joined: Jul 26, 2002
Posts: 5
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: May 06, 2003
Posts: 51
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Sep 24, 2003
Posts: 1608

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.


Tony Morris
Java Q&A (FAQ, Trivia)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60766
    
  65

Originally posted by Tony Morris:

(or niladic as it is sometimes called) constructor


I've also seen and used the term nullary constructor.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Niladic seems obscure: http://onelook.com/?w=niladic&ls=a
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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

Joined: Apr 01, 2006
Posts: 4
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

Joined: Oct 10, 2005
Posts: 110
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

Joined: Oct 25, 2005
Posts: 216
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Extending a class with private constructor
 
Similar Threads
Inheritance Question
When can a class not be extended?
constructor concepts...
what happen if all constructor of a class be declared as private?
OO basics doubt