aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes classes with private constructors can not be subclassed? 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 "classes with private constructors can not be subclassed?" Watch "classes with private constructors can not be subclassed?" New topic
Author

classes with private constructors can not be subclassed?

Pratibha Malhotra
Ranch Hand

Joined: Dec 21, 2003
Posts: 199
Following code gives a complie time error.
What I want to confirm is the fact that classes with private constructors can not be subclassed

[ September 06, 2005: Message edited by: Michael Ernest ]

~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,


Yes you are right....

if the constructor is private...and we dont have nay other overloaded constructor that is public ...


we cannot extend that class nad create an instance... as well as we cannot instnatiate that class..




[ September 06, 2005: Message edited by: A Kumar ]
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

You can't make instances of them either, unless there's a less restricted method available that can call the constructor.
[ September 06, 2005: Message edited by: Michael Ernest ]

Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
rituk Kapoor
Greenhorn

Joined: Sep 01, 2005
Posts: 4
Hi,

That's right. We can't either extend a class having only single private constructor nor can we create an instance of it. But if we will provide an alternative constructor as show below and call that constructor in subclass, then its possible.

Code:
---------------------------------------------------------------------------
import static java.lang.System.out;
class ABC{
private ABC(){}
}
public class Lang15 extends ABC{
public static void main(String args[]){
new ABC();
}
}
//Compilation Error: ABC() constructor not visible.

But if you will do the following thing:

import static java.lang.System.out;
class ABC{
private ABC(){}
public ABC(int i){}
}
public class Lang15 extends ABC{
public Lang15(int i){
super(i);
}
public static void main(String args[]){
new Lang15(12);
}
}

//It will compile.
Megs Maquito
Ranch Hand

Joined: May 18, 2005
Posts: 84
an example of a class with a private constructor is Math. It cannot be instantiated.


I'm a Hood Ornament
Joshua Smith
Ranch Hand

Joined: Aug 22, 2005
Posts: 193
Rewa-

It's common to use private constructors in utility classes (classes that have all static methods) for this very reason: to prevent instantiation. Some coding standards tools even enforce it.

Here's an short page describing this, and some other uses for private constructors:

Java Practices: Private constructor

Josh


Rational Pi Blog - Java, SCJP, Dev Bits- http://rationalpi.wordpress.com
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Originally posted by Rewa Dev:
Following code gives a complie time error.
What I want to confirm is the fact that classes with private constructors can not be subclassed


[ September 06, 2005: Message edited by: Michael Ernest ]


Just FYI... However, inner class of the top level class which has private constructor can be extended.



Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: classes with private constructors can not be subclassed?