2. introduction
Java provides a number of access modifiers to set access levels for classes,
variables, methods, and constructors.
1. Public
2. The Default . No Access Modifiers
3. Protected
4. private
3. Public Access Modifier
A class, method, constructor, data fields , etc. declared public can be
accessed from any other class. fields, methods, blocks declared inside a
public class can be accessed from any class in any package.
However, if the public class we are trying to access is in a different
package, then the public class still needs to be imported.
4. Public Access Modifier cont.
1.//save by A.java
2.
3.package pack;
4.public class A{
5.public void msg(){System.out.println("Hello");}
6.}
7.-------------------------------------------------------------------
1.//save by B.java
2.
3.package mypack;
4.import pack.*;
5.
6.class B{
7. public static void main(String args[]){
8. A obj = new A();
9. obj.msg();
10. }
11.}
12.------------------------------------------------------------------
Output:Hello
5. Default Access Modifier
Default access modifier means we don't explicitly declare an access
modifier for a class, field, method, etc.
A variable or method declared without any access control modifier is
available to any other class in the same package.
6. Default Access Modifier cont.
1. //save by A.java
2. package pack;
3. class A{
4. void msg(){System.out.println("Hello");}
5. }
6. ---------------------------------------------------------------
7. //save by B.java
8. package mypack;
9. import pack.*;
10. class B{
11. public static void main(String args[]){
12. A obj = new A();//Compile Time Error
13. obj.msg();//Compile Time Error
14. }
15. }
7. 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.
The protected access modifier cannot be applied to class . Methods,
fields can be declared protected.
8. Protected cont.
1. //save by A.java
2. package pack;
3. class A{
4. protected void msg(){System.out.println("Hello");}
5. }
6. -----------------------------------------------------------
7. //save by B.java
8. package mypack;
9. import pack.*;
10. class B{
11. public static void main(String args[]){
12. A obj = new A();//Compile Time Error
13. obj.msg();//Compile Time Error
14. }
15. }
9. Private
Methods, variables, and constructors that are declared private can
only be accessed within the declared class itself.
Private access modifier is the most restrictive access level. Class and
interfaces cannot be private.
Variables that are declared private can be accessed outside the class,
if public getter methods are present in the class.
Using the private modifier is the main way that an object encapsulates
itself and hides data from the outside world.
10. Private cont.
1. class A{
2. private int data=40;
3. private void msg(){System.out.println("Hello java");}
4. }
5.
6. public class Simple{
7. public static void main(String args[]){
8. A obj=new A();
9. System.out.println(obj.data);//Compile Time Error
10. obj.msg();//Compile Time Error
11. }
12. }
11. Note: A class cannot be private or protected except
nested class.
12. Access Control and Inheritance :
- The method, which is defined as a public in the superclass, must also be
public in all subclasses.
The method, which is defined as protected in the superclass, must also be
protected or public in subclasses. It can not be private.
The method that is defined without any access modifier in the superclass can
be defined in subclasses as private.
The method, which is defined as private, can not be executed by the
Inheritance process.
13. Access
Modifier
within class within package outside
package by
subclass only
outside
package
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y