-3

below are some nesty example of java access modifiers. I can't clearly figure out answers when classes have attributes whose type is other classes.

package packageX;
import packageY.*;

public class A {
    public int x;
    private int y;
    protected int z;

    public A objA;
    public C objC;

    void test(){
        System.out.println(x);
        System.out.println(y);
        System.out.println(z);

        System.out.println(objA.x);
        System.out.println(objA.y);
        System.out.println(objA.z);

        System.out.println(objC.x);
        System.out.println(objC.y);//not visible
        System.out.println(objC.z);//?? why visible?
    }
}

class B{
    public A objA;
    public C objC;

    void test(){
        System.out.println(x);//not visible
        System.out.println(y);//not visible
        System.out.println(z);//not visible

        System.out.println(objA.x);
        System.out.println(objA.y);//not visible
        System.out.println(objA.z);

        System.out.println(objC.x);
        System.out.println(objC.y);//not visible
        System.out.println(objC.z);//?? why visible?
    }
}



package packageY;
import packageX.*;

public class C extends A{
    public A objA;
    public C objC;

    void test(){
        System.out.println(x);
        System.out.println(y);//not visible
        System.out.println(z);

        System.out.println(objA.x);
        System.out.println(objA.y);//not visible
        System.out.println(objA.z);//not visible//??? why not visible?

        System.out.println(objC.x);
        System.out.println(objC.y);//not visible//??? why visible?
        System.out.println(objC.z);
    }

}

class D{
    public A objA;
    public C objC;

    void test(){
        System.out.println(x);//not visible
        System.out.println(y);//not visible
        System.out.println(z);//not visible

        System.out.println(objA.x);
        System.out.println(objA.y);//not visible
        System.out.println(objA.z);//not visible

        System.out.println(objC.x);
        System.out.println(objC.y);//not visible
        System.out.println(objC.z);//not visible//??? why not visible
    }
}

I can't understand few lines with //??? can you expain why the attributes are visible or not?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
pota
  • 1
  • 3

2 Answers2

1

Maybe it is a time to read some basic docs?

private int a - visible only in this class

protected int a - visible in this class and in all children classes extending this class

int a - visible from all classes in the same package as this class

public int a - visible from any other classes

Vadim
  • 4,027
  • 2
  • 10
  • 26
  • how about objA.z in class C. As C is subclass of A objA.z(protected) in C should be visible but it is not – pota May 16 '17 at 12:43
  • ObjA is not objC, only z belonging to objC is visible inside objC. Not from other objA. If you need it use either `public` or if classes are in the same package - package visibility with no modifier. – Vadim May 16 '17 at 13:01
0

This will explain Access https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

y is not visible in C because C extends A and A has made y private. Therefore C cannot see y.

z is protected so sub classes can see z.

Class B cannot see any of those variables because it does not inherit from any other class and it does not define those variables.

In class B objA.y can not be access because y is private. You can not access a private variable from outside the class that declares it.

objC.z is visible because protected members can be access by sub classes.

Read the link I provided. It explains this in the first table.

Allan
  • 2,889
  • 2
  • 27
  • 38
  • Yeah I know the case of x,y,z. What I wonder is the case of objA and objC. For example objC.z is visible in class A, but as I know C is defined in other package with A so objC.z(protected-> should be seen in same package and its subclasses) should be not visible in A – pota May 16 '17 at 12:35
  • but isn't A is super class of C? – pota May 16 '17 at 12:45
  • ObjC.z is inherited from A. z belongs to a. It is protected so it can be accessed by an instance of C. An instance of C can be created by any class and they can access z. Even if the super class creates an instance of the subclass.. – Allan May 16 '17 at 12:51
  • Thank! Then could you tell me why objA.y, objA.z is not visible in class C?? According to your comments, a instance of class A can access x, y and z(objA.x, objA.y, objA.z) however it's not in class C. – pota May 16 '17 at 13:05
  • D is not in the same package as A therefore protected members like Z are not accessible. Please read the link I provided it explains all of this with a simple example. – Allan May 16 '17 at 14:17