Re: Private or Protected access for super class variables
Best practice is to keep superclass variables private wherever possible (see below) and always initialise them through the superclass constructor.
Purists argue for always keeping all variables private. Keeping all variables private obviously means the subclass can't ever access them directly, so must access them indirectly through superclass methods that aren't private. This can make it tempting to provide protected get/set methods for the subclass to use. However, this is little better than direct acccess, and also allows other package classes access.
For the purist, encapsulation is all-important, and the superclass is treated like a separate class, so inheritance becomes a form of composition without the inconvenient forwarding methods (to do this via true composition would mean that the 'subclass' would have to implement the 'superclass' interface, and forward calls to those methods to the 'superclass' object).
Good designs need to be carefully though out in advance, and in the real world, the superclass variables may often be made 'protected' to allow subclass access, because it's inconvenient not to have direct access, or requirements have changed and it's too much effort to refactor the design. But in my experience, in large projects that are continually being extended and enhanced, this can lead to nasty problems where superclass variables are modified by subclasses without the superclass knowing (e.g. subclass sets a variable to a value the superclass doesn't expect, etc).
Inheritance is powerful, but should be used sparingly, and be kept as shallow as possible. Never subclass a concrete class, insert an abstract class if necessary. Unless the relationship is clearly and obviously A is a type of B, prefer composition over inheritance, using interfaces. Interfaces make your designs far more flexible.
Simplicity and flexibility will trump optimization and power in a world where connectivity is the key...
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.