access specifiers in java
what is access specifier ?
- Access specifier simply define how the members (attributes & methods) of a class can be accessed.
- The access identifier is a descriptive code that can determine which program items are allowed access to a particular type of data. The various programming languages ​​have their own rules of access agreements, as well as the default for some code elements including individual variables and categories.
access specifiers in java :-
- Public.
- Protected.
- Private.
- no access.
-> In java the default access specifier is no access.
-> In java the access specifier are implemented from a method, from a class, from a variable. it is fixed. by implementing the access specifier on a class, on a method, and on a variable. programmer will fix the uses of method and variable within the class, outside the class, within the package, and outside the package.
1. Public :-
When the member of the class is public then it is freely invoked within the class or outside the package either the class is inherited or not inherited.
2. Protected :-
When the member of the class is protected then it is freely invoked within the same package either the class is inherited or not inherited but a protected member invoked outside the package through inheritance.
3.Private :-
When the member of a class is private then it is only invoked in the same class.
A private member never be invoked outside the class.
4. no access :-
When the member of the class is no access then it is only invoked within the same package either the class is inherited or not inherited.
A no access member never be invoke outside the package.
Program for access specifier :-
In this case the package is LIPU & The class is A and i declare 4 members i.e. m1()(public), m2()(protected), m3()(no access), m4()(private) .
In this case i have declare same package (LIPU) and the class A1 extends to the class A and then i call the method m1()(public), m2()(protected), m3()(no access), the method m4()(private) is not call by another class,
In this case i have declare same package (LIPU) and the class A3 and then i call the method m1()(public), m2()(protected), m3()(no access), the method m4()(private) is not call by another class, and i declare the object by same method(i.e. A).
In this case i have declare the different package (java) and the class is X is extends to the previous class using class name . package number (i.e. LIPU.A ), and then i call the method m1()(public), m2()(protected), the method m3()(no access) is not call by another package, and also the method m4()(private) is not call by another package.
In this case i have declare the different package (java) and the class is x, and then when i declare the object then i call the previous class name with the help of package name .class name(i.e. LIPU.A) and then i call the method m1()(public), the method m2(), m3(), and m4() is not called by another package,
Post a Comment
Post a Comment