0

Studied in java that if a class have private constructor it cannot be inherited as super(), cannot be called,than why this code compiles, in case of inner class:

public class TestMe2 { 
    class Singleton { 
        private Singleton() { 
        } 
    } 
    class BB extends Singleton {  
    } 

    public void callMe(){ 
        Singleton sing=new BB(); 
    } 
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
Phalguni
  • 61
  • 1
  • 9
  • 1
    http://stackoverflow.com/questions/663059/why-do-inner-classes-make-private-methods-accessible – Jayan Jun 30 '14 at 16:58

3 Answers3

2

Because 2 inner classes of a common outer class have access to their respective private members.

Quote from the JLS:

A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
2

The private keyword limits your access to the constructor/fubction/variable it is refered to, in the same file. This means that in an inner class you actually have access to the private constructor/method/variable

To add to my answer, if your BB was in a different file you would have a problem and the java compiler would ask you to define another non-default constructor

edit: Mistakenly mentioning "same file". private keyword limits access to within the body of the top level class (§7.6) that encloses the declaration of the member or constructor

thanks @JB Niget

Klitos G.
  • 806
  • 5
  • 14
  • what do you mean by the same file, one file can have more than one java class, and the above code fails if the class are not inner class – Phalguni Jun 30 '14 at 17:17
  • You are correct, my mistake. The answer to your comment is given by @jb-niget answer Quote from the JLS: A private class member or constructor is accessible only within the body of the top level class (§7.6) that encloses the declaration of the member or constructor. – Klitos G. Jun 30 '14 at 17:20
0

BB is default constructed. The default constructor is package-private, and a class's constructor will always call the superclass constructor. Specifically:

If you don't do it explicitly yourself, there will be an implicit call inserted for you, no matter if the constructor is private or public.

So Singleton.Singleton will still be called by the subclass's constructor. And yes, this doesn't make a whole lot of sense compared to, say, C++, where private really does mean private.

You can call BB.BB because you're calling it from the same package.

Community
  • 1
  • 1
George Hilliard
  • 15,402
  • 9
  • 58
  • 96
  • The question is: why can BB call the Singleton constructor, since it's private? – JB Nizet Jun 30 '14 at 17:00
  • @JBNizet How was the question not answered? – BitNinja Jun 30 '14 at 17:01
  • The answer explains why you can call new BB(). It doesn't explain why the implicit BB cnstructor can call the private Singleton constructor, which is what the question is about. – JB Nizet Jun 30 '14 at 17:05