This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes private constructor inSuper Class? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "private constructor inSuper Class?" Watch "private constructor inSuper Class?" New topic
Author

private constructor inSuper Class?

nikunj jhala
Greenhorn

Joined: Nov 18, 2006
Posts: 9
when i have private constructor in super class,
& my base class inherit that super class,

class A
{
private A(){}
}

class B extends A
{
publc B()//error
{

}
}
class C
{
public static void main(String args[])
{
new C();
}
}

it will throw an error - private access in class A some what this type;

i know that super class construtor is called befor base class.so that this error occure. solN - i.e publlic or create other parameter constructor.

so tell me ,in real development where we can go for PRIVATE CONSTRUCTOR or we never used it???.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 682

I think that you should make the private constructor in class A private unless you have a public constructor that calls the private constructor:

class A {
private int i;
private A() {}
public A{int i} {
this.i = i;
this.A();
}
}

class B extends A {
public B() {
A(10);
}
}

-- Kaydell
[ November 25, 2006: Message edited by: Kaydell Leavitt ]
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12950
    
    3

Indeed, if the superclass only has private constructor(s) then you can't subclass that class, because the subclass needs access to a constructor of the superclass.

In "real development" you sometimes want to control the number of instances that are created of a specific class (for example using the singleton design pattern). You then make the constructor private and provide a factory method to get an instance of the class.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
nikunj jhala
Greenhorn

Joined: Nov 18, 2006
Posts: 9
Originally posted by Kaydell Leavitt:
I think that you should make the private constructor in class A private unless you have a public constructor that calls the private constructor:

class A {
private int i;
private A() {}
public A{int i} {
this.i = i;
this.A();
}
}

class B extends A {
public B() {
A(10);
}
}

-- Kaydell

[ November 25, 2006: Message edited by: Kaydell Leavitt ]

your code generate error;

such as

C.java:8: cannot find symb
symbol : method A()
location: class A
this.A();
^
1 error

so i have return your code with main method
class A {
private int i;
private A() {}

public A(int i)
{
this.i = i;
this.A();
}
}

class B extends A {
public B() {
super(10);
}
}

class C
{
public static void main(String args[])
{

B x=new B();

}

}

i dont know when we are going to use Private constructor???

thanks for Reply....
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 682

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by nikunj jhala:
so tell me ,in real development where we can go for PRIVATE CONSTRUCTOR or we never used it???.


In "real development" a private constructor might be used for many different reasons. First, it may be used to suppress the "default" constructor in a class that should never be instantiated such as java.lang.Math. Second, it may be used in cases where the constructor should never be called directly. Third, it could be used when no public constructors are provided and a static factory method is provided instead. There's many reasons you might choose to do (or not do) any of these things so without a more specific question I won't endeavor to explain them.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: private constructor inSuper Class?
 
Similar Threads
Explicit default constructor
constructor
sample experimanting mock code(consturctor related)
question about constructor
Question on Construtor