Access Specifier vs Access Modifier Java

Access Specifier vs Access Modifier Java


Access Specifier vs Access Modifier: For a novice, both the words specifier and modifier looks one and the same and may be confusing. If you go by the literal meaning, it is more confusing, say, an access specifier specifies the access and access modifier modifies the access. Of course, if the meaning and functionality is known clearly, it is not confusing at all. Let us explain practically with examples.

A) Access Specifier – Meaning

Observe.

1. public void display() { }
2. private void display() { }

There are two display() methods with public and private. These two words give permissions to other classes to access display() method. public means any class from anywhere can access (like a public park; any one can enter or leave without ticket). private means not accessible to other classes; the method can be used by the same class (in which it is declared, like a private property used by the same family for which it belongs). Now I think it is clear. The public and private are known as access specifiers because they specify the access.

There are four access specifiers Java supports, public, protected, default (not specified at all) and private with different access restrictions (permissions). Infact, these four are keywords also.

B) Access Modifier – Meaning

Observe the following code.

In the above code, the display() method of Test class is overridden by Demo class. Infact, Demo class is at liberty to override or not. Now let us apply a small modifier to display() method of Test class. See the following code.

In the super class Test, observe, the display() method is added with final keyword. In the super class, if a method is declared as final, it cannot be overridden by subclass. That is, super class by declaring method as final does not allow the subclass to override. This is the modification given to the method in super class with final. final is known as access modifier.

Now let us see one more modifier to have better understanding. Observe the following code.

Access Specifier vs Access Modifier

In Demo class, marks is a non-static variable and price is static variable. marks (non-static variable) requires object to call from main() method where as static variable price does not require object. So, now what is the modification static gave. static modifies the access and gives the facility to call without the help of an object.

I am sure, you are very clear with the difference between specifier and modifier.

Access specifiers and access modifiers are applied to variables, methods and classes. Note, you cannot apply all specifiers to all (variables, methods and classes) and all modifiers to all. That is, for example, a local variable cannot be static and a variable cannot be abstract. Similarly, a class cannot be static.

Following links gives you the indepth knowledge of both.

1. Access Modifiers
2. Access Modifiers – Meanings
3. Access Specifiers
4. Java Private variable accessibility
5. Rules of Access Specifiers in Method Overriding

4 thoughts on “Access Specifier vs Access Modifier Java

  1. satish

    in Student class i overrides the equals(),hashcode(),tostring(),
    i used the HashSet type.here when i am adding objects to hashSet
    HashSet ignoring Equals(),hashcode() in Student class why Plz Tel me sir

    1. S. Nageswara Rao, Corporate Trainer Post author

      HashSet uses its own overridden methods and not yours. Suppose you extend my Employee class containing overridden methods of the above. You extend my Employee class and in your class you override the same methods. If you create an object Employee class, does it take my overridden methods of my code or yours.

Leave a Reply

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