JAVA

All Languages C CPP JAVA HTML CSS JAVASCRIPT PYTHON

Modifier Types Java

Modifiers are keywords which changes themeaning. The types of modifiers are: access modifiers and non-access modifiers.
The access modifiers are keywords which specify the access policies on the member and the class+.

Four types of access modifiers:

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

The non-access modifiers are static, final, abstract.

Private:

Example:

 public class PrivateDemo
{
    private int age;
    private String color;
    private void test()
    {
          System.out.println(“Inside test method”);
    }
}
public class Run
{  
     public static void main(String args[])
     {  
      PrivateDemo obj=new PrivateDemo();  
      System.out.println(obj.age);//Compile Time Error  
      obj.test();//Compile Time Error  
     }   
}

Default:

Example:

Two packages firstpack and secondpack are created. Here UserMsg class is not public, so it cannot be accessed from outside the package.

//save as UserMsg.java  
package firstpack;  
class UserMsg
{  
  void msg()
  {
    System.out.println("Hello User");
  }  
} 

//save below as Demo.java  
package secondpack;  
import firstpack.*;  
class Demo
{  
    public static void main(String args[]){  
		UserMsg obj1 = new UserMsg ();//Compile Time Error  
		obj1.msg();//Compile Time Error  
	}  
} 

Protected:

The protected member is accessible within package and outside the package but there should be inheritance only.

Example:

Two packages firstpack and secondpack are created. Here UserMsg class is not public, so can be accessed from outside the package. But msg method is declared as protected, so it can be accessed from outside the class only there is inheritance.

//save as UserMsg.java 
package firstpack; 
class UserMsg
{ 
 protected void msg()
 {
 System.out.println("Hello");
 } 
} 
  
 //save by Demo.java 
package secondpack; 
import firstpack.*; 
class Demo extends UserMsg{ 
 public static void main(String args[])
{ 
 Demo obj = new Demo(); 
 obj.msg(); 
 } 
} 

When you compile and run this program, you will get

Output

Hello

Public:

Modifiers which can be access anywhere in the project.

Example:

 //save as UserMsg.java  
package firstpack;  
class UserMsg
{  
 public void msg()
  {
	System.out.println("Hello");
  }  
}  
//save as Demo.java 
package secondpack; 
import firstpack.*; 
class Demo{ 
public static void main(String args[])
{ 
 Demo obj = new Demo(); 
 obj.msg(); 
 } 
} 
 

Understanding all java access modifiers:

Non Access Modifiers:

Java has non-access Modifiers are:

Static:

The members(Data member and member Fuction) which are declared by using static keyword are called static members.

When ever the JVM encounters the class name first time, it loads all the static members to static pool.

Example:

public class StaticExm
{
	staticinttotSum = 0;
	publicstaticvoidadditionNum(int a, int b)
	   {
		intmethodSum = a+b;
		System.out.println("The adding of two number: "+methodSum);
	   }
	public static void main(String[] arguments) 
	{
		StaticExm.totSum=10;      // static data-member accessing by using class name
		StaticExm.additionNum(5, 6); // Static method accessing by using class name					
		StaticExm s1= newStaticExm(); //creating Object of class
	    s1.totSum = 22;    //static data-member accessing by creating instance/object
	    s1.additionNum(10, 30); //static method accessing by creating instance/object
	}
}
 

The output:

Final:

keyword is used to finalise the behaviour or initialiazation of a member.

Example:

package firstpack;
public class FinalVariableDemo{
 finalintuserAge=10;
 public static final int votingAge=18; //Declaring constant
 public static void main(String args[])
 { 
     FinalVariableDemofd = new FinalVariableDemo();
     Fd.userAge=18;//error: cannot be re-initialise
 }
}

Final Methods:

Example:

public class UserName {
   public final void Name()
    {
      System.out.println(“User Name”);
    }
}
 

Final Classes:

A class is declared as final then we cannot inherit any feature from the final class.

Example:

public final class UserName
{
 int i=0;
 public void test()
 {
 //body of test method
 }
}

Abstract: is a keyword used to make method, class as abstract.

Abstract Method:

Syntax:

Abstract public void testName(); // abstract method

Abstract Class:

Example:

abstract class UserDetails{
 private String Name;
 private int age;
 public abstract void getDetails();//an abstract method
 public void educationDetails() // concrete method
 {
     System.out.println(“education details”);
 }
}