Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private constructor inheritance

 
Rupika jain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a class with a private no argument constructor can be extended.

I run this code:

class base{
private base(){}
public base(int i){
System.out.println("i am in base args");
}
}

public class Foo extends base{

public static void main(String args[]){
Foo o = new Foo();
}
}

I get an error: cannot find symbol
symbol : constructor base()
location: class javaapplication1.base


I understand that since the consructor is private and can only be called from same class, does it also means that any class with a private no-args constructor cannot be extended.
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

jain
-------------------------------------------------------------------------
does it also means that any class with a private no-args constructor cannot be extended.
---------------------------------------------------------------------------
It is not like that
Foo o = new Foo();//line 1

Because of the above statement[line 1] you are getting compile time error.Because by default it is like this

Foo()
{
super();
}

When you say new Foo();
A call to super class no-arg constructor will be there,because the constructor is private the compiler knows that,it gives compile time error.

If you want replace the code with

Foo()
{
super(10);
}

Now the code will compile and executes.

Thanks

Anil Kumar
[ May 31, 2007: Message edited by: anil kumar ]
 
Gunjan Kumar
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does it also means that any class with a private no-args constructor cannot be extended.


I dont think so, Here is one example

class base{
private base(){}
public base(int i){
this();
System.out.println("i am in base args");
}
}

public class Foo extends base{

public static void main(String args[]){
Foo o = new Foo();
}

public Foo()
{super(5);}

}
 
Pravin Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a constructor in Foo class with int parameter
like public Foo(int i) { super(i); }
then the Foo class will compile
[ May 31, 2007: Message edited by: Pravin Jain ]
 
Neha Monga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rupika!

If a class has both private and public constructors then that class can be extended. But private superclass constructor should not be called from sub class as its not visible in subclass. In order to get your code compiled correctly, you need to make sure that you chain constructors appropriately such that private superclass constructor is not called from sub class though it can very well be called from another super class constructor.

But if a class has just one explicit default/non-default constructor declared as PRIVATE, then that class cannot be extended.Compiler will complain if you extend that class. Such a class is known as 'Singleton'.

Hope the above explanation suffices.

Ranchers, pls correct me if I am wrong.

Thanks
Neha Monga
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Exception to the rule that a class with private constructor can't
be extended. It is all about inner classes.



Output:
Inn1 constructor
Inn2 constructor


Neha, your description fits alright to the top level classes.

Thanks,
 
Neha Monga
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Chandra, the code provided by you compiles fine and runs...

Would you know why an inner class with private constructor can be subclassed?

Thanks!
Neha Monga
 
Sergio Tridente
Ranch Hand
Posts: 329
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Neha,

Beacuse a (non-static) inner has access to all members of the outer class, even if those members are private.
 
Rupika jain
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes i got the concept. Thanks to all of you.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Would you know why an inner class with private constructor can be subclassed?


Inner classes (not nested also called non static) have access to all the
members of the enclosing class. It would go against rule if an inner class
with private constructor could not be extended by another inner class of
the same enclosing class.



Thanks,
 
Xing Xai Ma
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent explaination by chandra,
and very good point by the call the to private constructor using THIS() in super constructor.

my doubt - inner constructor chain call to private constructor would result in compile time error.

- just want to make sure this point that i found unexplored.
- thank you once again especially mr. chandra and other folks for discussing very important topic. waiting for the flag
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic