java - When i need to use a protected access modifier -


this question has answer here:

my question concerning "protected" access modifier.

i know functionality, don't know when need use it. conceptually methods in class divided as: constructors setters/getters methods used clients (i.e other classes) internal methods (used other methods in class)

you use protected when

  • your class designed inheritance - expect users of library inherit class designing. class abstract.
  • the class provides special functionality derived classes must not visible other classes - know derived classes must have access information otherwise private, or
  • the derived classes must provide functionality base class - see template method pattern information use of protected methods.

note protected methods similar public methods in sense once put them in, need stay in long support library. unlike private methods can freely remove, protected methods remain part of interface of class.


Comments