What is difference between access modifier and access specifier in java

in Java
Java Tutorials
2 Answers to this question

Access Specifier:- This can be understood as the access you provide to your code in Java whether other classes can access your code or not.

E.g. public, private, protected and default.

Access Modifier:- Java provides both Access Specifier and Access Modifiers for creating access to your Java code for other classes. Here modifier is also used to do the same task but there are limitations.

Class Modifier:

abstract :- This defines the restriction such that objects cannot be created.

final:- This restricts a class from being inherited.

strictfp:- it is related to the checking of floating point values irrespective of OS.

Variable Modifier:

static:no object creation required

final: cannot be reassigned

transient: it is not serialized

volatile: the values are liable for change


This concept is introduced in C++ . In C++ keywords (Reserved words) are categorized in two categories like access specifiers and access modifiers. Now, we will see keywords categorization in access specifiers and access modifiers.

•Access Specifiers: private, public,protected and default. These four keywords are introduced in C++ as access specifiers.

•Access Modifiers: static, abstract , synchronize, strictfp, native etc. All keywords except private, public, protected and default are introduced in C++ as access modifiers.
For Example: With the help of an example we can see that private is not an access modifier in C++.


#include <iostream>

using namespace std;

class AccessSpecifers{

private:

int x ;



};

int main(){



AccessSpecifers ob;

ob.x = 10;

}


Error:

Main.cpp:12:8: error: 'x' is a private member of 'AccessSpecifers'

ob.x = 10;


Explanation:


In the above program, we are getting an error “ 'x' is a private member of 'AccessSpecifers' ” due to a private member. As we all know, private members can’t be used outside of class “AccessModifiers”. But we have to confirm whether “private” is introduced in c++ as access modifiers or not. So it is clear “private” is not considered as access modifiers.

 

 

But, In Java there is no such concept or such terminology as access specifiers that means all keywords(Reserved words) are categorized in one and only one category i.e. access modifiers.

•Access Modifiers: private, protected, public, default, static, synchronize, abstract, native, strictfp etc. All keywords are introduced in java as access modifiers.

 


For Example: With the help of an example we can see that private is access modifiers in java.


private class AccessModifiers {

public class Main{

 

}

}


Error:

/Main.java:1: error: modifier private not allowed here

private class AccessModifiers {

^

1 error


Explanation:

In the above program, we are getting an error “modifier private not allowed here” due to private modifier. As we all know, private modifiers can’t be used with a top level class named “AccessModifiers”. But we have to confirm whether “private” is introduced in java as access modifiers or not. So it is clear “private” is considered as access modifiers.

 

If you want to unleash your potential in this competitive field, please visit the Java Certification Training page for more information, where you can find the       and    Java Interview Questions FAQ's and answers    as well.

For more updates on the latest courses stay tuned to HKR Trainings.

To Top