What is Accessibility modifiers?
The keywords which define accessibility permissions are called accessibility modifiers.
Java supports four accessibility modifiers.
- private
- protected
- public
- default(no keyword)
Access Specifier | Within Class | Within Package | Subclass in Another Package | Anywhere |
---|---|---|---|---|
private | allowed | not allowed | not allowed | not allowed |
protected | allowed | allowed | allowed | not allowed |
public | allowed | allowed | allowed | allowed |
No Access(Default) | allowed | allowed | not allowed | not allowed |
How private works?
The class members which have private keyword are called private members.
If we declare any variable or method with private accessibility modifier then those variables and methods are accessible only within that class , not accessible outside the class.
Example
class Student{ private int registerNumber=40; } public class Student1{ public static void main(String args[]){ Student obj=new Student(); System.out.println(obj.registerNumber); } }Try it Yourself
How protected works?
The class members which have protected keyword are called protected members.
protected variables or method accessible inside the package anywhere.
Outside package accessible only in sub classes.
Example
package easy; public class Employee{ protected void msg(){ System.out.println("This method is accessible within the package and subclass in another package"); } } package mypack; import easy.*; class Manager extends Employee{ public static void main(String args[]){ Manager obj = new Manager(); obj.msg(); } }
How public works?
If we declare any variable or method with public access specifier then those members will be accessible to everywhere.
Example
package easy; public class Hello{ public void msg(){ System.out.println("This method is accessible from anywhere"); } } package mypack; import easy.*; class Welcome{ public static void main(String args[]){ Hello obj = new Hello(); obj.msg(); } }
How default works?
If we declare any member with no keyword those members are called default members.
default members are accessible to package level.
Means we can access anywhere in same package but we can not access in out side the package under any condition.
So default will acts as public inside package and private out side the package.
Example
package easy; class Hello{ void msg(){ System.out.println("This method is accessible within the package"); } } package mypack; import easy.*; class Welcome{ public static void main(String args[]){ Hello obj = new Hello(); obj.msg(); } }
Solve this!!
class access{ public int x; private int y; void cal(int a, int b){ x = a + 1; y = b; } } class access_specifier { public static void main(String args[]){ access obj = new access(); obj.cal(2, 3); System.out.println(obj.x + " " + obj.y); } }
Exception in thread “main” java.lang.Error: Unresolved compilation problem: The field y has private access.
class access{ public int x; private int y; void cal(int a, int b){ x = a + 1; y = b; } void print(){ System.out.println(" " + y); } } class access_specifier { public static void main(String args[]){ access obj = new access(); obj.cal(2, 3); System.out.print(obj.x); obj.print(); } }
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling