Professional Documents
Culture Documents
As the name suggests access modifiers in Java helps to restrict the scope of a
class, constructor, variable, method, or data member. There are four types of access
modifiers available in java:
1. Default – No keyword required
2. Private
3. Protected
4. Public
3.Algorithm/Flowchart:
1. Create a package
2. Create a class within the package
3. Create a method inside the class
4. Save the code by the name of the class.java
5. Create another package i.e.acess
6. Import all classes of package created at step 1
7. Create a class inside mypack package
8. Create main method inside this class
9. Create object of a class of package imported at step 6
10. Call the method which exists inside that another class with scope defined as
public .
Experiment:- 1.4
4. Steps for experiment/practical/Code:
For Default
1. package accessmodifiier;
2. class A{
3. void cal(){
4. System.out.println("Hello");
5. }
6. }
1. package accessD;
2. import accessmodifiier.*;
3. class B{
6. obj.call();
7. }
8. }
For Private
1. class A{
2. private int data=10;
3. private void call(){
4. System.out.println("Hello World");
5. }
6. }
For Public
1. package accesspu;
2. public class A{
3. public void call(){
4. System.out.println("Hello");
Experiment:- 1.4
5. }
6. }
1. package acesspu;
2. import accesspu.*;
3. class B{
4. public static void main(String args[]){
5. A obj = new A();
6. obj.call();
7. }
8. }
Output:-
5. Observations/Discussions/ComplexityAnalysis:
1. Private: The access level of a private modifier is only within the class. It cannot be
accessed from outside the class.
2. Default: The access level of a default modifier is only within the package. It cannot
be accessed from outside the package. If you do not specify any access level, it will
be the default.
3. Protected: The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child class, it cannot
be accessed from outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed
from within the class, outside the class, within the package and outside the package.
Experiment:- 1.4
6.Learning outcomes:
• • Access Modifiers in Java are used to define the accessibility of methods and
data members.
• • There are four main types of Access Modifiers – Default, Private, Protected,
and Public.
• • Each modifier has varying levels of restrictions. The Public Modifier has the
least restrictions, whereas the Private Access Modifier has the most limitations.
• • If there is no Access Modifier defined, it is treated as a Default Access
Modifier.