Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

classes with private constructors can not be subclassed?

 
Pratibha Malhotra
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
rituk Kapoor
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an example of a class with a private constructor is Math. It cannot be instantiated.
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic