2

I use Boyarskiy OCA certification book, page 173.

So, I faced with some issue: I have parent class Bird with protected field name and child in other package.

package bird;

public class Bird{
    protected String name;
}

and child:

package swan;
import bird.Bird;

public class Swan extends Bird{
   public void check1(){
      System.out.print(name);
   }

   public void check2(){ 
      Swan swan = new Swan();
      System.out.print(swan.name);
   }

   public void check3(){
      Bird bird = new Bird();
      System.out.print(bird.name);
   }
}

So, -check1 compile ok

-check2 compile, but why? check2() is create new instance of class and try to call parent field name from other package(not via inheritance).

-check3 doesn't compile.

Srinivasan Sekar
  • 2,049
  • 13
  • 22
Roberto
  • 1,288
  • 5
  • 23
  • 47
  • Jeanne Boyarsky has written a few books. Please post the name of the one your are using. – Abra May 20 '19 at 10:19
  • 1
    I think the JLS will provide an answer. By the way, just a nitpick but nothing is trying to "call parent field name" - it's trying to **access** the field. You can't call fields, you can only call methods. I'm only mentioning this because you mentioned certification, and if you're taking a certification test it's important to know the correct terminology. – DodgyCodeException May 20 '19 at 10:21
  • @Abra Oracle certified OCA java 8 – Roberto May 20 '19 at 10:39

2 Answers2

2

The topic is handled inside the Java Language Specification chapter 6.6.2: https://docs.oracle.com/javase/specs/jls/se12/html/jls-6.html#jls-6.6.2.1

The example inside there is, that the Class C (Bird in your example) has a protected member and S (Swan in your example) is a subclass of C.

"If the access is by (i) a qualified name of the form ExpressionName.Id or TypeName.Id, or (ii) a field access expression of the form Primary.Id, then access to the instance field Id is permitted if and only if the qualifying type is S or a subclass of S."

Your function check2 fits into this definition, because you have swan.name and swan is of type "S" (if we keep the names C / S of the standard).

So check3 has to fail because the qualifying type is not S (Swan in your example) or a subclass of S (it is of type C - "Bird" - in this used szenario).

Konrad Neitzel
  • 736
  • 3
  • 7
  • 2
    If methods in `Swan` did not have full access to all fields in any other `Swan` instances, you could not implement `equals` (such as `return this.name.equals(otherSwan.name)`). – Thilo May 20 '19 at 10:27
  • I also forgot to mention: The JLS also references: "More information about access to protected members can be found in Checking Access to Protected Members in the Java Virtual Machine by Alessandro Coglio, in the Journal of Object Technology, October 2005." which I got through Google at http://www.jot.fm/issues/issue_2005_10/article3.pdf if I didn't miss anything. Maybe some people prefer to read such an article and not the JLS. – Konrad Neitzel May 20 '19 at 10:31
1

protected is a way of saying private for outside world, public for children. The way a family has secrets that the world should not know about. That's why check1 and check2 compiles, because the child has access. However, for check3, you're trying to access something that only a child/someone from the family has access, so you're not allowed.

More details in the official documentation for OOP and access control: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Silviu Burcea
  • 5,103
  • 1
  • 29
  • 43