aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what happen if all constructor of a class be declared as private? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what happen if all constructor of a class be declared as private?" Watch "what happen if all constructor of a class be declared as private?" New topic
Author

what happen if all constructor of a class be declared as private?

james gong
Ranch Hand

Joined: May 29, 2001
Posts: 48
A class which has all its constructors declared as private
1.Cannot be instantiated by any other class.
2.Cannot be extended.
3.Both i and ii.
4.has to be declared final.
key is ;2
but i cant pull out myself from ans1 and ans2,and I also cant make me clearly among the two ans.concern the case please say something .
tvs sundaram
Ranch Hand

Joined: Jan 28, 2001
Posts: 153
Instantiation is only for interfaces.
For classes it is extending only.
HTH
tvs sundaram.
Guoqiao Sun
Ranch Hand

Joined: Jul 18, 2001
Posts: 317
In my opinion, both 1 and 2 are correct.
Any better idea?
Guoqiao
Originally posted by james gong:
A class which has all its constructors declared as private
1.Cannot be instantiated by any other class.
2.Cannot be extended.
3.Both i and ii.
4.has to be declared final.
key is ;2
but i cant pull out myself from ans1 and ans2,and I also cant make me clearly among the two ans.concern the case please say something .


Guoqiao Sun<br />SCJP2 SCWCD2<br />Creator of <a href="http://www.jiris.com/" target="_blank" rel="nofollow">www.jiris.com</a>, Java resource, mock exam, forum
james gong
Ranch Hand

Joined: May 29, 2001
Posts: 48
I think the ans1 that "Cannot be instantiated by any other class" are right ,but I am not so claerly about it ,somebody can clearfy my confusition about the concept.
Namrata Shetty
Ranch Hand

Joined: Jul 18, 2001
Posts: 30
i think it's both (i) & (ii)
(i) because if a class has all its constructors private then u cannot create an instance of the class anywhere except in that class itself.

Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi all,
A class having only private ctors can be instantiated if it contains a static method which can used to create and return an instance of itself.
I'm guessing that's what the author of the question had in mind.
Hope that helps.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It can't be instantiated by any other class. It can be instantiated by itself through a public method but instantiation implies the use of "new" and new can't be used if all the constructors are private.
It can't be extended because the extending class wouldn't be able to instantiate a copy of the parent class.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Two quotes from the JLS
8.8.8 Preventing Instantiation of a Class
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. A public class can likewise prevent the creation of instances outside its package by declaring at least one constructor, to prevent creation of a default constructor with public access, and declaring no constructor that is public.
So can't be instantiated

6.6.8
A private class member or constructor is accessible only within the class body in which the member is declared and is not inherited by subclasses.
So i guess i can't extend a class with a private constructor either .
Correct me if i'm wrong .
[This message has been edited by Ashish Hareet (edited August 03, 2001).]
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
James,
The correct answer is 3.
You cannot instantiate a class which has all constructors private outside the class.You would usually find such classes with most of the methods declared as static.
If you are aware of Design Patterns, Singleton Pattern is based on this concept.This pattern states that you can get only one instance of the class and all the objects would share this instance.
For example :

Note how the other class gets an instance of the class which has all its constructors declared private.Also, note that the publicly defined method of this class is called.
The other correct answer is that the class with all private declared constructors cannot be extended.This is because, your subclass constructor by default gives a call to its superclass constructor using a super().You would get an error, since the subclass will not be able to find the superclass constructor in this case.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Thomas,
Originally posted by Thomas Paul:
It can't be instantiated by any other class. It can be instantiated by itself through a public method but instantiation implies the use of "new" and new can't be used if all the constructors are private.
It can't be extended because the extending class wouldn't be able to instantiate a copy of the parent class.

You can use "new" keyword, even if the class has all privately defined Constructors.However, you need to use it in the class itself.
Please see the code that I have posted earlier in this thread.
Hope this helps,
Sandeep
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Solution: class cannot be instantiated und extended
class Ct1
{
private Ct1() {System.out.println("SuperclassConstructor");}
}
class Ct2 extends Ct1
{
Ct2() {super(); System.out.println("SubclassConstructor");}
}
class Cttest
{
public static void main (String argv[])
{
Ct1 instct1 = new Ct1();
Ct2 instct2 = new Ct2();
}
}

C:\jdk1.3\bin>javac cttest.java
cttest.java:8: Ct1() has private access in Ct1
Ct2() {super(); System.out.println("SubclassConstructor");}
^
cttest.java:14: Ct1() has private access in Ct1
Ct1 instct1 = new Ct1();
^
=> Private constructor cannot be accessed outside the class
=> No instance outside the class Ct1 cannot be created (e.g.
for MAIN-Class Cttest and it cannot be extended.
Thomas
2 errors
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Desai Sandeep:
Thomas,
You can use "new" keyword, even if the class has all privately defined Constructors.However, you need to use it in the class itself.
Please see the code that I have posted earlier in this thread.
That's what I meant when I said that, "It can't be instantiated by any other class. It can be instantiated by itself through a public method..."
Bob Vel
Greenhorn

Joined: Mar 21, 2001
Posts: 24
class Test1{
private Test1() {}
static Test1 makeATest1() {
return new Test1();
}
}
public class Test2{
public static void main(String[] args) {
//!Test1 x = new Test1();
Test1 x = Test1.makeATest1();
}
}
//!Test1 x = new Test1();
You cannot create a Test1 object via its constructor. Instead you should call the makeaTest1() method to do this.
So, only answer 2 is applicable. Hope this will help.
If I am wrong then someone please correct it.
Regards
Bob

Without involvement, there is no commitment. Mark it down, asterisk it, circle it, underline it.<BR>No involvement, no commitment.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
But technically, it isn't being instantiated by another class. It is being instantiated by itself upon the request of another class.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
I think Jane's answer toward the beginning of this thread was correct and succinct.
The real power of the idea that a class must instantiate itself is the Singleton design pattern. It is useful in a lot of situations.
The Singleton idea can be extended to support pooling of objects (a Singleton being a pool of 1 object).
Have fun, Guy
Ken Lai
Ranch Hand

Joined: Aug 06, 2000
Posts: 30
Originally posted by james gong:
A class which has all its constructors declared as private
1.Cannot be instantiated by any other class.
2.Cannot be extended.
3.Both i and ii.
4.has to be declared final.
key is ;2


Sounds right to me.
Notice that answer 1 doesn't specify whether the other class is inside or outside the class with the private constructor. Please take a look at the code below and let me know what you think (I don't really know what would be the use of similar code though).

class Ex
{
public static void main(String args[])
{
Ex.SubEx sx = new Ex().new SubEx();
sx.method();
}//main

private Ex(){}
class SubEx
{
SubEx()
{ new Ex() ;
}
void method()
{
System.out.println(this);
}
}//class SubEx
}//class

[This message has been edited by Ken Lai (edited August 10, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what happen if all constructor of a class be declared as private?