The correct Answer is C, because class (even if it is outside the package) extends from the class with the protected attribute, it will have access to it. So access is by package and by inheritance.
Incorrect:
Field r (protected) is accessible in the subclass (Test) via inheritance. However, it is not accessible via the object reference obj because obj is of type Acc, not Test. In Java, protected members are only accessible directly through inheritance or within the same package.
Correct ans is B:
Even though Test is child of ACC we are not able to access protected r variable. Reason is packages are different. If you want to access protected member in different package then it can be accessed only using "Child class object" not using parent class or Child class reference hold by parent.
For protected r to be visible need to create object of Test t=new Test ; int pr=t.r;
Here ACC obj=new Test //We are calling protected using parent class reference hence invalid. If protected r is in same package then it would be valid .
Incorrect Field r (protected):
It is accessible in the subclass (Test) via inheritance. However, it is not accessible via the object reference obj because obj is of type Acc, not Test. In Java, protected members are only accessible directly through inheritance or within the same package.
Correct answer is B.
C - isn't correct, this is a tricky question.
This case is shown in the book "OCA: Oracle® Certified Associate Java® SE 8 Programmer I Study Guide Exam 1Z0-808" by Jeanne Boyarsky, Scott Selikoff
Although the object instanciation happens to be in a Test class (subclass of Acc), it is stored in a Acc reference. We are not allowed to refer to members of Acc class since we are not in the same package of Acc;
The answer is the letter C, the variable "r" is protected and it can be accessed directly by another class if the child class extends the mother even though they are in different packages.
To test:
------------------------
// Acc.java
package p1;
public class Acc {
int p = 0;
private int q = 1;
protected int r = 2;
public int s = 3;
}
------------------------
// Test.java
package p2;
import p1.Acc;
public class Test extends Acc {
public static void main(String[] args) {
Acc obj = new Test();
System.out.println(obj.p);
System.out.println(obj.q);
System.out.println(obj.r);
System.out.println(obj.s);
}
}
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
iSnover
Highly Voted 2 years, 4 months agoTheeOne
5 days, 6 hours agoRu_H33
Highly Voted 1 year, 11 months agojp_ofi
Most Recent 1 week, 2 days agovic88
2 months agoMPignaProTech
2 months, 3 weeks agoTheeOne
5 days, 6 hours agoKrok
7 months, 1 week agodeyvi25
9 months, 2 weeks ago7df49fb
10 months, 1 week agoyassineRaddaoui
1 year, 11 months agotawa_z58
1 year, 11 months agoAnkit1010
1 year, 11 months agoodzio33
2 years agohaisaco
2 years, 1 month agoanmoldev2java
2 years, 2 months agojimcoun
2 years, 3 months agoiSnover
2 years, 3 months agocarloswork
2 years, 3 months agocarloswork
2 years, 2 months ago