-3

So I know this is a beginner question, but I got confused, because from what I understand is that protected is only accessible to sub-classes / classes that extend the super class .. but I'm working on a project and weirdly enough, I was able to access a protected instance variable from class A , in class B (class B does not extend class A). Can someone please explain this?

Faisal
  • 35
  • 3
  • Read [The Java™ Tutorials](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) – Andreas Apr 02 '18 at 19:37

1 Answers1

2

Yes it is. As long as the classes are in the same package, protected is accessible, even if they are non-sub classes. But when a class wants to access protected from a different package, then protected is only available to sub-classes.

TheEngineer
  • 792
  • 6
  • 18