difference between protected and package-private access modifiers in Java? [duplicate]

The first answer is basically correct – protected members can be accessed by classes from the same package subclasses of the declaring class from other packages However, there is a little trick: 6.6.2 Details on protected Access A protected member or constructor of an object may be accessed from outside the package in which it …

Read more

Reasons to use private instead of protected for fields and methods

There is some consensus that one should prefer composition over inheritance in OOP. There are several reasons for this (google if you’re interested), but the main part is that: inheritance is seldom the best tool and is not as flexible as other solutions the protected members/fields form an interface towards your subclasses interfaces (and assumptions …

Read more