Java | Access Modes

05 min read

Access Modes:

 

 

A class member which is either data member or member function can be declared with any of the following access mode specifiers:

  1. private - A private member is accessible only within the class, in which it is declared that is member functions within that class, can only access them.
  2. public - A public member can be accessed even outside the class in which it is declared.
  3. protected - A protected member can be accessed by the class in which it is declared and by its derived classes.
  4. default or 'package-private' - It is the additional access mode specifier to the above three access modes as it is introduced in Java. Java is a true object-oriented programming language. So everything in Java is implemented as an object. When a variable is declared without any access specifier it assumes that as default access mode which means it can be accessed by all the classes defined within that package. But the classes outside the package cannot access it. This means a default member is public to all the classes within the same package, in which it is defined. Hence the name 'package-private' mode.

 

 

POST A NEW COMMENT
     
  • Input (stdin)

    Output (stdout)


    Input (stdin)

    Your Output (stdout)

    Expected Output

    Compiler Message

    Input (stdin)

    2    3

    Your Output (stdout)

    5

    Expected Output

    5

    Compiler Message

    5

    Error