2

A friend of mine asked this question to me. Why the following code does not give error on invoking aa.x()?
I understand that aa is a reference to object of class B but is invoking private method of class A inside the method of class A where it is visible and hence accessible.

Is my understanding correct? Or is there any other reason behind this?

public class A {
    public void xyz() {
        System.out.println("A");
    }
    private void x() {
        System.out.println("A:x");
    }
    public static void main(String[] args) {
        B b = new B();
        A aa = b;
        aa.x();
        aa.xyz();
        B bb = (B) aa;
        bb.xyz();
        bb.xyz12();
    }
}
class B extends A {
    public void xyz() {
        System.out.println("B");
    }
    public void xyz12() {
        System.out.println("B-12");
    }
}
Abhash Upadhyaya
  • 717
  • 14
  • 34
  • 4
    So many duplicates, so little time... *Edit:* Er, well, maybe not with the subclass wrinkle. – T.J. Crowder Sep 17 '15 at 17:12
  • If you want it to understand please read about access modifiers here https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html. private access modifier is for Class and main is in this class so there is no problem to call that method – Gaskoin Sep 17 '15 at 17:13
  • Here one might as well write `A aa = new A()` instead of `A aa = b`. Since `A aa = b` is legal and `aa.x()` is legal inside `A#main()`, everything ok. – Victor Sorokin Sep 17 '15 at 17:15
  • Your understanding is correct. If it was a public method, it would have been inherited by Class B and during runtime the class B's method would have got executed. If the method is not inherited and only present in B then compiler throws error. – Prudhvi Sep 17 '15 at 17:19

3 Answers3

3

I can't immediately find a duplicate using a subclass, but fundamentally it's the same answer as the answer to this question.

There are two things that govern access to x:

  1. Where the code is that's doing the access. Since x is private to A, the code accessing it must be part of a method in A. It can't be in a subclass (B) or an unrelated class.

  2. What kind of reference you're using. If you have an A reference, you can access x on it. If you have a B reference, you can't, even though your code is part of an A method. You could cast it to A and then access x, but you can't do it directly with a reference of type B.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 3
    It's less about where the code is located and more about the compile time type of `aa`. You wouldn't be able to do `b.x()` inside `A`. Runtime types don't matter. Scope and accessibility is all about compile time. – Sotirios Delimanolis Sep 17 '15 at 17:17
  • @SotiriosDelimanolis: That's true *too*, but if you have an `A` reference `a`, you can't do `a.x()` in non-`A` code, either, – T.J. Crowder Sep 17 '15 at 17:18
  • Sure, but I don't think that's what they're asking. – Sotirios Delimanolis Sep 17 '15 at 17:19
  • @SotiriosDelimanolis: *"...is invoking private method of class A inside the method of class A where it is visible and hence accessible..."* Where teh code is that's being run seems pretty applicable to me. – T.J. Crowder Sep 17 '15 at 17:22
  • The issue can be simplified since having `B` here is irrelevant (as it extends `A` and thus is an `A`). It's the exact same question that you reference. – Kenney Sep 17 '15 at 17:23
  • @Kenney: Which is why I linked it, in a CW answer. But they *aren't* the same question, just closely-related. So Sotirios points out, there's the question of the reference type. – T.J. Crowder Sep 17 '15 at 17:24
  • @T.J.Crowder How did you add the answer to community wiki? – Prudhvi Sep 17 '15 at 17:25
  • @prudhvi: You have that option when adding an answer (or editing your own answer), it's a tickbox under the text area. – T.J. Crowder Sep 17 '15 at 17:27
  • Haven't delved into CW yet, but I am agreeing with you. As far as I can tell they are the same question, once you remove the irrelevant code from OP's question. – Kenney Sep 17 '15 at 17:28
0

It is only visible because the main method where it is invoked is contained in class A. Move it to class B and it will not work

ControlAltDel
  • 33,923
  • 10
  • 53
  • 80
0

As the private methods are not inherited, a superclass reference calls its own private method.

Your main method is the method of A, therefore it can can call x() private method.

private modifier—the field is accessible only within its own class.

Vladislav Kievski
  • 1,637
  • 11
  • 12