Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (6.5k points)
What are the differences between the specifiers?

1 Answer

0 votes
by (11.3k points)

Java provides the programmer with a multitude of options to adjust the accessibility of an element in his code:

  • Public: The public specifier is used when there isn't a requirement for emphasis on security where the particular method, class or field is concerned. The elements defined by this access specifier and can be accessed by all elements of the program. 
  • Protected: These can be accessed within the same class, by the subclasses or within the classes of the same package that the container class or protected class belongs to.
  • Default: When an access specifier hasn't been explicitly specified, it is called a default class. These can be accessed within the same package.
  • Private: Only accessible within the same class. These are not visible in their subclasses either.

    

All of these access specifiers have different roles in a real-world scenario and are widely used in a production environment. If you're looking to learn java, you should make a habit of using all these classes according to the use-case which is presented to you.  

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...