Access Modifiers

  • public static void main(String args[])
  • public is an access modifier means that we can set the level of access.
  • we can use the access modifier for classes, attributes & methods.

For a classes the available modifiers are public or default( left blank ) , as described below:-

  • public : The class is accessible by any other class .
  • default : The class is accessible only by classes in the same package.
  • The following choices are available for attributes and method :-
  • default:  A variable or method declared with no access control modifier is available to any other class in the same package.
  • public:  Accessible from any other class.
  • protected: provides the same access as  the default access modifier , with the addition that subclasses can access protected methods and the variables of superclass (subclass and superclasses are covered).
  • private:  Accessible only within the declared class itself.
  • Example:-
    public class vehicle{
         private int maxSpeed;
         private int wheels;
         private String color;
    
        public void horn(){
           System.out.println("Beep!!");
       } 
    }

Next-