Access Modifier – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Thu, 07 Jul 2022 16:00:33 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg Access Modifier – Programmerbay https://programmerbay.com 32 32 Difference between Access Modifier and Access Specifiers in Java https://programmerbay.com/difference-between-access-modifier-and-access-specifiers-in-java/ https://programmerbay.com/difference-between-access-modifier-and-access-specifiers-in-java/#respond Thu, 07 Jul 2022 10:25:57 +0000 https://programmerbay.com/?p=8800 Both access modifiers and access specifiers are interchangeably used in Java. “access specifiers” is more commonly used in C++, but in Java, “access modifier” is considered as official term that developers use for private, public and protected.

It can be viewed as a synonym of access modifier. They represent the concept of access control .
Therefore, there is no difference between an access specifier and an access modifier.

So, there is no difference between access specifier and access modifier.

What is access specifier in Java?

An access specifier, officially known as access modifier, determines how a class and its member can be accessed within a class or same package or in different packages. Java supports four access modifiers private,public,protected and default ( no modifier) or package-private.

Java different access modifiers:

There are 4 types of access modifier, explained below:

1) Public : This access modifier is applicable to both a class and its members. It signifies that a class defined with public access modifier can be accessible to all other classes within or outside the package.

2) Default: When no modifier is explicitly provided to a class or its member, then Java treats it as default or package-private. The accessibility of this type is limited to the same package only.

3) Protected: This modifier can only be used on members of a class. It allows other classes to access the respective members of the class within the same package and different packages using a subclass.

4) Private: This is the highest level of restriction a class and its member can have. It allows access to members within a class only in which it is defined.

Java Default access specifier or access modifier

When a class is declared without providing an access modifier explicitly, it is treated as default or package-private. It enables a class with this modifier to be only visible or accessible within the same package, not outside a package.

It designates the same behavior even if a subclass tries to access its superclass’s members in a different package.

class A {

void getMessage(){
System.out.println(" I am accessible");
}
}

class B {
public static void main(String[] args) {
A obj = new A();
obj.getMessage(); // Accessible within same package

}
}

Java Public access specifier or access modifier

The “Public” access modifier provides the widest accessibility scope. It can be used to declare a class or method public using public keyword. If a class or its member is declared as public, then they could be accessible anywhere in the code.

In other words, no restriction is enforced on the visibility of the respective class or its member. Below is a program example for the same.

class A {

public void getMessage(){
System.out.println(" I am accessible");
}
}

class B {
public static void main(String[] args) {
A obj = new A();
obj.getMessage(); // Accessible throughout

}
}

 

Java Protected access specifier or access modifier

The “protected” access modifier provides higher accessibility after the Public modifier. It is defined using a protected keyword to a member of a class. It permits access to other classes within the same package and subclasses belong to different packages.

class A {

protected void getMessage(){
System.out.println(" I am accessible");
}
}

class B {
public static void main(String[] args) {
A obj = new A();
obj.getMessage(); // Accessible within same packages

}
}

 

Java Private access specifier or access modifier

The “private” access modifier is the most restricted access modifier in Java as it only allows access of a member to a class in which it has declared. It can be used by adding the “private” keyword to a member of a class.

class A {

private void getMessage(){
System.out.println(" I am accessible");
}
}

class B {
public static void main(String[] args) {
A obj = new A();
obj.getMessage(); // compile time error : getMessage() has private access in A

}
}

 

]]>
https://programmerbay.com/difference-between-access-modifier-and-access-specifiers-in-java/feed/ 0