1

Someone asked me if he can run private base function. I told him that of course it is not possible (apart from trick of relection). But what the hell is this:

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

        A a = new B();
        a.doSomething();

        B b = new B();
        b.doSomethingMore();
    }

   static class A {
        private void doSomething(){
            System.out.println("something");
        }
    }

    static class B extends A{
        public void doSomethingMore(){
            ((A)this).doSomething();
        }
    }
}
Vishwa Ratna
  • 5,567
  • 5
  • 33
  • 55
Zixav
  • 131
  • 1
  • What exactly is not clear about the code? – Stultuske Feb 12 '19 at 11:57
  • 5
    Your method is private to the class `MyClass`. It can be called from any code inside `MyClass`. – khelwood Feb 12 '19 at 11:58
  • `private` access level class YES https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – m0skit0 Feb 12 '19 at 11:58
  • Your inner classes A and B are in the same scope. If you remove B from MyClass, the visibility will be hidden, and you cannot call the private method. – rockfarkas Feb 12 '19 at 12:13
  • 1
    I wouldn't be surprised if this is a duplicate, but I wouldn't call it a duplicate of [this marked question](https://stackoverflow.com/questions/6989494/why-am-i-able-to-call-private-method) (and certainly not of [this PHP question](https://stackoverflow.com/questions/27066518/why-can-you-call-a-private-method-from-outside-of-the-object-scope)!). – T.J. Crowder Feb 12 '19 at 12:13
  • @rockfarkas - I already did. :-) (Note that the PHP question had been mistagged [tag:java] as well as [tag:php], I fixed that as well...) – T.J. Crowder Feb 12 '19 at 12:30
  • https://stackoverflow.com/questions/215497/what-is-the-difference-between-public-protected-package-private-and-private-in Public, protected, no modifier, private – FailingCoder Feb 12 '19 at 13:51

1 Answers1

2

A and B are both members of MyClass, so they have access to all of MyClass's private features, and to each other's private features; and MyClass has access to all of their private features. More in the Java nested classes tutorial.

Now, if they weren't nested classes, then naturally MyClass wouldn't have access to their private features, and they wouldn't wouldn't have access to each others' private features. For instance, this won't compile:

public class MyClass {
    public static void main(String args[]) {
        A a = new B();
        a.doSomething();            // error: doSomething() has private access in A

        B b = new B();
        b.doSomethingMore();
    }
}

class A {
    private void doSomething(){
        System.out.println("something");
    }
}

class B extends A{
    public void doSomethingMore(){
        ((A)this).doSomething();    // error: doSomething() has private access in A
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I can understood MyClass having access to his childs, but B having access to A's doSomething have no sense at all. Well thanks for your answer. – Zixav Feb 12 '19 at 12:07
  • 1
    @Zixav - I get why you'd think that. :-) But the point of nested classes, the design philosophy behind them, is that they're really *part of* the class they're in. So **all** of the private stuff is shared between them. (But I could see the design decision going either way, frankly. This is just the way it went, 24+ years ago... :-) ) – T.J. Crowder Feb 12 '19 at 12:11