Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Quiz #46

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class which has all its constructors declared as private
1. Cannot be instantiated.
2. Cannot be extended.
3. Both i and ii.
4. has to be declared final.
Answer given is 3.
This is wrong since a class with a private constructor can be instantiated as follows:

public class Test
{
private Test()
{
}
public static void main(String[] argv)
{
Test t = new Test();
}
}
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JLS 8.6.8


A class can be designed to prevent code outside the class declaration from creating instances of the class by declaring at least one constructor, to prevent the creation of an implicit constructor, and declaring all constructors to be private.


Emphasis here is to the clause code outside the class.
Here indeed, the main method belongs to the same class. So
it compiles and works fine.
HTH
Ajith
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My choice here is
2. Cannot be extended.
Regds
- satya
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Satya,
IMO -
My understanding is, if the constructor(s) is/are not visible
for the code outside the class ( by the virtue of being private ), you will not be able to extend the class.
Here's why. The subclass constructor(s) calls the super class constructor either implicitly or
explicitly. Now, because the subclass encompasses
a completely different scope than the superclass,
the subclass code is foreign for the superclass.
That's what JLS calls as code outside the class.
Actually (ii) is implied by (i) which is
inturn implied by all the constructor(s)
being private.
Do you agree with me ???
Ajith
[This message has been edited by Ajith Kallambella (edited May 03, 2000).]
[This message has been edited by Ajith Kallambella (edited May 03, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ajith:
FYI...My post was in the context of Ali's question.
Also, I agree with you about the explanation of
whats considered "outside" the class.
However, I disagree with your comment about
"Actually (ii) is implied by (i) which is
inturn implied by all the constructor(s)
being private."
Consider the statement "(ii) is implied by (i)".
I don't think so.
Reason: All class that cannot be extended need not
mean that they cannot be instantiated.
Final classes cannot be extended either, but may be
I could instantiate them if I have a constructor.
Hence I would not say (ii) is implied by (i).
Now consider the other half of the statement
"which is inturn implied by .... "
I disagree with this also (in the context of the
question asked).
Reason: I can instantiate the class inside the class.
I think while reading the question you are assuming to be
"outside" the class, while the question doesnot mention
anything like that.
Hope I am right.
Regds.
- satya
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point Satya. I think my implies
went a little overboard. Thanks for your
clarifications.


Ajith
 
please buy my thing and then I'll have more money:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic