Saturday, 25 December 2021

Access specifier or protections in java

An access specifier is a keyword that is used to specify how to access a member of a class or the class itself. It means, we can use access specifiers with the members of a class or with the class also.

There are four access specifiers in java: private, public, protected and default 

In the  following figure, we are taking three classes, class-A, class-B, and class-C. Where class-A and class-B are present in the same package, while class-C in another package.

Private members of class-A are not available to class-B or class-C. This means that, if any class contain private members, they are available only within that class only.So scope of private members ( variable and methods) is class scope.

Public members of class-A are available to class-B and also class-C. This means that, if any class contains public members, they can use every where when ever necessary.So scope of public members ( variable and methods) is global scope.

Protected members of class-A are available to class-B, but not in class-C. But, if, class-C is a sub class of class-A, then the protected members of class-A are available to class-C. 

When no access specifier is used, it is taken as default specifier. Default members of class-A are accessible to class-B which is within the same package. They are are not available to class-C. This means, the scope of default members is package scope.


0 comments:

Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

Computer Basics

More

C Programming

More

Java Tutorial

More

Data Structures

More

MS Office

More

Database Management

More
Top