• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instantiation is only for interfaces.
For classes it is extending only.
HTH
tvs sundaram.
 
Guoqiao Sun
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .

 
james gong
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Desai Sandeep
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic