Modifiers in Java


 Modifiers in java

There are two types of modifiers in java.

1.Access modifiers

2.Non-access modifiers.

1.Access modifiers

Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:

  1. Visible to the package is No modifiers are required.
  2. Visible to the class only (private).
  3. Visible to the world (public).
  4. Visible to the package and all subclasses (protected).

The access modifiers in java specify accessibility (scope) of a data member, method, constructor or class. Mean that there are 4 types of java access modifiers:

  1. private
  2. default
  3. protected
  4. public

1)  private access modifier :

The private access modifier is accessible only within class

Example

In this example, we have created two classes Abc and xyz. Abc class contains private data member and private method. We are accessing these private members from outside the class, so there is compile time time error.

class Abc

{

private int a=10;

private void get()

{

System.out.println(“welcome java”);

}  }

public class xyz

{

public static void main(String args[])

{

Abc obj=new Abc();

System.out.println(obj.a);   //Compile Time Error  

obj.get();   //Compile Time Error

}  }

Role of Private Constructor

If you make any class constructor private, you cannot create the instance of that class from outside the class. For example

class Abc

{

private Abc()

{

//private constructor

}

 

void get()

{

System.out.println(“Hello java”);

}

}

public class xyz

{

public static void main(String args[])

{

Abc obj=new Abc(); //Compile Time Error  

}

}

Note: A class cannot be private or protected except nested class.

2) default access modifier:

If you don’t use any modifier, it is treated as default by default. The default modifier is accessible only within   package.

Example

In this example, we have created two packages pack and pack1. We are accessing the Abc class from outside its package, since Abc class is not public, so it cannot be accessed from outside the  package.

//save this program by Abc.java

package pack;

class Abc

{

void get()

{

System.out.println(“Hello”);

}

}

//save by B.java

 

package pack1;

 

import pack.*;

class Bcd

{

public static void main(String args[])

{

Abc obj = new Abc();   //Compile Time Error

obj.get();     //Compile Time Error

}

}

In the above example, the scope of class Abc and its method get() is default so it cannot be accessed from outside the package

3) protected access modifier:

The protected access modifier is accessible within package and outside the package but through inheritance only. The protected access modifier can be applied on the data member, method and constructor. It can’t be applied on the class.

Example

In this example, we have created the two packages pack and pack1. The A class of pack package is public, so can be accessed from outside the package. But msg method of this package is declared as protected, so it can be accessed from outside the class only through inheritance.

//save by Abc.java

package pack;

public class Abc

{

protected void get()

{

System.out.println(“Hello Java”);}

}

//save by B.java

package pack1;

import pack.*;

class Bcd extends Abc

{

public static void main(String args[])

{

B obj = new B();

obj.get();

}

}

Output: Hello Java

 

4) public access modifier:

The public access modifier is accessible everywhere. It has the widest scope among all other modifier.

Example

//save by Abc.java

package pack;

public class Abc

{

public void get()

{

System.out.println(“welcome”);

}

}

//save by Bcd.java

 

package pack1;

import pack.*;

class Bcd

{

public static void main(String args[])

{

Abc obj = new Abc();

obj.get();

}

}

Output:welcome

Understanding all java access modifiers

Let’s understand the access modifiers by a simple table.

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

 

Comments are closed.