coderz.py

Keep Coding Keep Cheering!

Access Modifiers in Java

method overloading and method overriding

Modifiers in Java are divided into two categories: access modifiers and non-access modifiers.

The accessibility or scope of a field, method, constructor, or class is defined by the access modifiers in Java. The access modifier can be used to change the access level of fields, constructors, methods, and classes.

Types of access modifiers:

There are four types of Java access modifiers:

  • Private: A private modifier has only one level of access: within the class. It is not accessible outside the class.
  • Default: A default modifier’s access level is limited to the package. It is not accessible from the outside of the package. If you don’t specify an access level, the default will be used.
  • Protected: A protected modifier’s access level is both within and outside the package via a child class. The child class cannot be accessed from outside the package unless it is created.
  • Public: A public modifier’s access level is universal. It can be accessed from inside and outside the class, as well as from within and outside the package.
Access Modifierwithin classwithin packageoutside package by subclass onlyoutside package
PrivateYesNoNoNo
DefaultYesYesNoNo
ProtectedYesYesYesNo
PublicYesYesYesYes
1) Private

The private access modifier is accessible only within the class.

Example:

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

If no modifier is specified, it is assumed to be the default. The default modifier is only available within the package. It is not accessible from the outside of the package. It allows for more access than private. However, it is more restricted than protected and open.

Example:

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

The protected access modifier can be used both inside and outside of the package, but only through inheritance.

The data member, method, and constructor can all have the protected access modifier applied to them. It is not applicable to the class.

It is more accessible than the standard modifier.

Example:

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

The public access modifier can be used anywhere. It is the most versatile of all the modifiers.

Example:

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

Note: also read about the Constructor in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement