File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private constructor inheritance 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 "Private constructor inheritance" Watch "Private constructor inheritance" New topic
Author

Private constructor inheritance

Rupika jain
Greenhorn

Joined: May 31, 2007
Posts: 4
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

Joined: Feb 23, 2007
Posts: 447
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

Joined: Mar 26, 2007
Posts: 74
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);}

}


Gunjan Kumar
SCJP 1.5
Pravin Jain
Ranch Hand

Joined: May 20, 2007
Posts: 60
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 ]

The Zen of Java Programming.
Neha Monga
Ranch Hand

Joined: Mar 13, 2007
Posts: 34
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

Joined: Feb 28, 2007
Posts: 1707
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,


cmbhatt
Neha Monga
Ranch Hand

Joined: Mar 13, 2007
Posts: 34
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

Joined: Mar 22, 2007
Posts: 329

Hi Neha,

Beacuse a (non-static) inner has access to all members of the outer class, even if those members are private.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Rupika jain
Greenhorn

Joined: May 31, 2007
Posts: 4
Yes i got the concept. Thanks to all of you.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707

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

Joined: Sep 12, 2006
Posts: 4
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private constructor inheritance
 
Similar Threads
doubt in constructor...
Explicit default constructor
doubt regarding constructors
private constructor in abstract classes
Question on Inheritance syntax