Using Protected Access Modifier Correctly in Java
The attributes or methods with protected access modifier in parent class are inherited in subclass, hence the subclass can access them. When the subclass or parent class object is created in a class that reside in different package, we can not access the protected attributes from the object reference. So protected access modifier works like private as far access from user-classes (i.e. where we instantiate an object of that class) are concern. It works like public as far inheritance is concern, as its accessible in subclass code. That is why it comes between private and public access. So here comes a fundamental question, should we make our instance attributes protected if we want to make them available to subclasses? From good software design perspective, the straight answer is BIG NO, we should not make the instance attributes protected even if we want to make them inheritable, why? For the same reason as discussed in previous sections i.e. it breaks the encapsulation principle,...