• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
April 16, 2024 |17.7K Views

What are access modifiers in Java

  Share  2 Likes
Description
Discussion

As the name suggests access modifiers in Java helps to restrict the scope of a class, constructor, variable, method, or data member. There are four types of access modifiers available in java: 

  1. Default – No keyword required
  2. Private
  3. Protected
  4. Public
  • Default: When no access modifier is specified for a class, method, or data member – It is said to be having the default access modifier by default.
    • The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.

In this example, we will create two packages and the classes in the packages will be having the default access modifiers and we will try to access a class from one package from a class of the second package.

 

Related Article :  https://www.geeksforgeeks.org/access-modifiers-java/