public vs. protected vs. private

Screen Shot 2016-07-24 at 2.21.46 PM

  • public: A class, method, constructor, interface etc declared public can be accessed from any other class.
  • protected: Variables, methods and constructors which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members’ class.
  • no modifier: A variable or method declared without any access control modifier is available to any other class in the same package.
  • private: Methods, Variables and Constructors that are declared private can only be accessed within the declared class itself.

Leave A Comment

Your email address will not be published. Required fields are marked *