1

Considering following class definition.

public final class engine{
protected Object _obj=new Object();
}

My question is, as final classes are not allowed to be inherited in java, so the protected access specifier rules like private here.
Why doesn't java raise error for this definition while code compilation?
Is it possible to inherit from Engine using reflection, native codes, anything?

  • 3
    You can also access a protected variable from within the package (in this case protected is the same as default). Also; raising an error in this case would be a massive overreaction, a warning maybe – Richard Tingle May 25 '14 at 11:20
  • 3
    Read more about it here [Why are protected members allowed in final java classes?](http://stackoverflow.com/questions/3661270/why-are-protected-members-allowed-in-final-java-classes) – Braj May 25 '14 at 11:25

1 Answers1

1

Protected means that you can reach that variable from the same package or subclass. Private means that you can only reach that variable from the class it is declared in..

See the reference: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

AdamK
  • 177
  • 10