• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Extending a class with private constructor

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic