Access Modifiers
In java we use access modifiers to control visibility of class and class members (Like instance variable, method, constructor)
Java works around objects and these objects call each other's methods. Lets see this by two examples
Example 1 :- Providing access
Suppose you have a method to add two numbers. This is kind of utility and anyone wants to use this method if it is already written. So this should be visible and available for everyone's use.Example 2:- Restricting access
You have bank account class and in that you have balance as instance variable. Now in this case it is okay for everyone to see balance of your bank account. But it is really not okay that someone can modify your account balance.So here you need to restrict everyone from modifying your account balance except bank.
Types of Access Modifiers
There are four types of access modifiers in java.- public access modifier
- private access modifier
- protected access modifier
- default access modifier
Note:-
You must remember one thing, all 4 types are applicable to class members. But only public and default access can be given to a class.This means that a class can not be private or protected. This will give you compile time error.
Lets see about all 4 access modifiers in Java programming one-by-one
Public Access
When a class member is declared public then all other classes can access that member, regardless of the package that class belong to. This also assumes that class should also be visible.Example :-
public class MyMathClass {public int add(int a, int b){
int c = a + b;
return c;
}
}
Now any class can access this add method simply by making object of MyMathClass like
MyMathClass mathObj = new MyMathClass();
mathObj.add(2,4);
Private Access
Class members which are marked private can not be accessed outside of that class.
Private class members will also not be visible by inheritance. They are exclusive to that class only.
Example:-
public class MyMathClass {
private int add(int a, int b){
int c = a + b;
return c;
}
public static void main(String args[]){
int result = new MyMathClass().add(2,4);
System.out.println(result);
}
}
Here add method can only be accessed by MyMathClass and can not be accessed beyond this by any means.
Protected Access
Protected members can be accessed (through Inheritance) by sub class even if sub class belongs to different package.
Example:-
package com.mypackage.demo;
public class MyMathClass {
protected int add(int a, int b){
int c = a + b;
return c;
}
}
Another Class
package com.yourpackage.demo;
public class YourMathClass extends MyMathClass {
public void testIt() {
int result = add(2,4);
System.out.println(result);
}
}
class YourMathClass can access protected method inside MyMathClass even if they belong to different packages.
Default Access
Default members can be accessed only if the class accessing the member belongs to the same package.
Example:-
package com.mypackage.demo;
public class MyMathClass {
int add(int a, int b){
int c = a + b;
return c;
}
}
Another Class
package com.mypackage.demo;
public class YourMathClass {
public void testIt() {
int result = add(2,4);
System.out.println(result);
}
}
Here add method has default access and it can be accessible by other classes in same package.
Hope you will find that useful. You can put your queries in comments.
0 Comments
Post a Comment