Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

private constructor inSuper Class?  RSS feed

 
nikunj jhala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nikunj jhala
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!