Loading

Modifier

    Keypoints

  • Modifiers are those keywords that come before the declaration of the class, interface, and its members.
  • It changes the behavior of the code;. 
  • In the below example, the public is a modifier of the class and it's informing that the given JavaClass is accessible everywhere in the application. We will deeply study the modifier further in this chapter. 


public class JavaClass{
    //here public is a modifier
} 

Types of Modifiers

  • There are two types of modifiers 

  1. Access Modifiers 
  2. Non-Access Modifiers

1. Access Modifiers:-

Access modifiers are those keywords that describe the rule of the access level of Classes, interfaces, and their members( i.e. - constructors, methods, variables, etc. )


Example:

package quipohouse;
class HelloWorld {
	public void method1 () {….}     //It can be access by other class
	private void method2 () {….}    //It can’t be access by other class
	protected void method3 (){….}   //It can be access within the pakcage and by its child       	        
}

Types of Access Modifiers

There are Four types of access modifiers :

1. Default Access Modifier or package private:-

If we don't mark any access modifier( like public, private, protected) before declaring a class, interface, method, or other class members, the Java compiler by default gives the access within the package where it is declared.

Key Points:

  • Class declared without a modifier is called as default access modifier.
  • It can be accessed only within the package.

Example:

//File Name: Main.java

package quipohouse;
class Message
{
  void msg()
  {
    System.out.println ("Hello to QUIPO HOUSE World");
  }
}

public class Main
{
  public static void main (String[]agrs)	//main method always have pubic access-modifier.
  {
    Message obj = new Message ();
    obj.msg ();
  }
}

Output:

Hello to QUIPO HOUSE World

2. Public Access Modifier:-

  • The class interface and its members declared public and can be accessible everywhere.  
  • It has no scope restriction.

Example:

//File 1 : HelloWorld.java

package quipohouse;
public class HelloWorld {

  public void message() {

    System.out.println(“Welcome to Quipo House”);
  }
}
//File 2 : Main.java

package quipohouse;
public class Main
{
  public static void main(String[] args)
  {
    HelloWorld obj = new HelloWorld ();
      obj.message ();
  }
}

Output:

Welcome to Quipo House

3. Private Access Modifier:-

  • Class member marker as private cannot access outside its class.
  • Except for the nested class, A class cannot be private.

Example:

//File : Main.java

package quipohouse;
class HelloWorld
{
private void message()
    {
        System.out.println("Quipo House");
    }
}
public class Main
{
public static void main(String args[])
    {
        HelloWorld obj = new HelloWorld ();
        //Trying to access private method message from another class
        obj.message();
    }
}

Output:

Main.java:17: error: message() has private access in HelloWorld
        obj.message();

Example of the Access level of private modifier with class members (method, variable, etc.):

//File : Main.java

package quipohouse;
class Message
{
  private String msg;
  public String getMessage ()          // getter method
  {
    return this.msg;
  }
  public void setMessage (String msg)   // setter method
  {
    this.msg = msg;
  }
}

public class Main
{
  public static void main (String[]main)
  {
    Message m = new Message ();

      // accessing the private variable using the getter and setter method
      m.setMessage ("Welcome To Quipo House");
      System.out.println (m.getMessage ());
  }
}

Output:

Welcome To Quipo House

4. Protected Access Modifier:-

  • When the methods and variables are declared with a protected access modifier, we can access them within the same package as well as from its subclasses even outside the package where it is declared.
  • A class cannot be protected.

Example:

//File : HelloWorld.java

public class HelloWorld
{

  protected void message ()
  {
    System.out.println ("Welcome to Quipo House ");
  }
}
//File : Main.java

public class Main extends HelloWorld
{
  public static void main (String[]args)
  {
    HelloWorld obj = new HelloWorld ();
      obj.message ();
  }
}

Output:

Welcome to Quipo House		

Top-level Classes cannot be private and protected.

Access ModifiersClassPackageoutside the package
PrivateYesNoNo
DefaultYesYesNo
ProtectedYesYesOnly in the Derived class
PublicYesYesYes

2. Non-Access Modifier :-

The Non-access modifier affects the code behaviour other than its visibility or scope.

 There are six non-access modifiers. We will separately study it further in this tutorial.

  • static: It refers to memory management, its members the part of its class rather than objects.
  • final: We can assign the class variable method with the final keyword. A final class cannot be inherited. The final variable cannot change its value once it gets initiated. The final method cannot be overridden.
  • abstract: It is used to achieve abstraction.
  • synchronized: It is used to access control of the thread method.
  • transient: It skips to serialize the object.
  • volatile: To read the value of a variable from the main memory.