How to Java programming- Access Modifiers

There are three access modifiers. Not including default access modifier. The default modifier is an access control which will be set when one does not specify any access modifier.

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

There are many non-access modifiers such as static, abstract, synchronized, native, volatile, transient etc.

 

1.private Access modifier.

A variable or method is defined private will be accessible only from within the Class in which it is defined. Such variables are not accessible from outside the defined Class, not even in its subclass.

Private Access Modifier
	class ExamplePrivate{  
	private int data=40;  
	private void msg(){System.out.println("Hello java");}  
	}  
	  
	public class Simple{  
	 public static void main(String args[]){  
	   ExamplePrivate obj=new ExamplePrivate();  
	   System.out.println(obj.data);//Compile Time Error  
	   obj.msg();//Compile Time Error  
	   }  
	}

 

Output: Compile error!

 

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 the package.

Default Access Modifier
	public class Clock {
		long time = 0;
	}
	public class ClockReader {
		Clock clock = new Clock();

		public long readClock{
			return clock.time;
		}
	}

 

The time field in the Clock class has no access modifier, which means that it is implicitly assigned the default/package access modifier. Therefore, the ClockReader class can read the time member variable of the Clock object, provided that ClockReader and Clock are located in the same Java package.

 

3.protected Access modifier

If a variable or method is set to protected inside a Class, it will be accessible from its subclasses defined in the same or different package. The protected access modifier is accessible within the package and outside the package but through inheritance only.

In below the example, we have created two packages package1 and package2. In package1, class A is public so we can access this class outside of package1 but method show is declared as a protected so it is only accessible outside of  package1 only through inheritance.

Protected Access Modifier
	package package1;  
	public class A{  
		protected void show(){
			System.out.println("Hello Java");
		}  
	}
Protected Access Modifier
	package package2;  
	import package1.*;  
	class B extends A{  
		public static void main(String args[]){  
			B obj = new B();  
			obj.show();  
		}  
	}  

 

Output
      Hello Java

 

4.public Access modifier.

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

A.java
	//A.java

	package pack;  
	public class A{  
		public void msg(){System.out.println("Hello");}  
	}
B.java
	//B.java  
	  
	package mypack;  
	import pack.*;  
	class B{  
	  public static void main(String args[]){  
		 A obj = new A();  
		 obj.msg();  
	  }  
	}

 

Output
     Output:Hello

 

Anwar Yakkiparamban

Anwar Yakkiparamban is the founder of Lauyou Learning. Prior to Lauyou learning, Anwar worked at ARD Engineering & Development, Qatar. He holds bachelor degree in Electronics and Communication Engineering from Govt. Engineering College Idukki.

You may also like...