Public class A1 { public int x; private int y; protected int z; … } public class A2 extends A1 { protected int a; private int b; … } pub

Question

Public class A1 { public int x; private int y; protected int z; … } public class A2 extends A1 { protected int a; private int b; … } public class A3 extends A2 { private int q; … } Which of the following lists of instance data are accessible in class A2?
1. a, b
2. x, y, z, a, b
3. x, z, a, b
4. x, y, z, a
5. z, a, b

in progress 0
Rose 4 days 2021-10-10T04:58:47+00:00 1 Answer 0

Answers ( )

    0
    2021-10-10T05:00:12+00:00

    Answer:

    The correct option is option 3.

    Step-by-step explanation:

    The public variable is accessible to all the classes

    A private variable is accessible to only the class in which it is defined

    A protected variable is accessible in the defining and derived classes

    Now as the class A2 is under consideration, so the two variables which are defined in A2 are both accessible by A2 which are the protected int a and the private int b.

    Now for the other variables, public int x is accessible to all the classes as it is a public variable.

    Private int y is defined in class A1 so it is not accessible to class A2.

    Protected int z is defined in class A1 but A2 is derived from A1 thus this is also accessible by A2.

    Private int q is defined in class A3 so it is not accessible to class A2.

    Thus a,b,x and z are the four variables accessible by the class A2 sso option 3 is the correct option.

Leave an answer

45:7+7-4:2-5:5*4+35:2 =? ( )