0% found this document useful (0 votes)
18 views

Practical-1.4: Write Program To Study Access Modifiers in Java Using Packages

The document describes a Java programming assignment to study access modifiers using packages. It outlines creating classes with different access modifiers - default, private, protected, and public - across multiple packages and observing the results. The key steps are to create classes in one package and call them from another package with different access modifiers. Observations show which access levels allow access within and across classes and packages. The learning outcomes are how to use access modifiers with packages in Java and understand the requirements of each access modifier.

Uploaded by

prerna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Practical-1.4: Write Program To Study Access Modifiers in Java Using Packages

The document describes a Java programming assignment to study access modifiers using packages. It outlines creating classes with different access modifiers - default, private, protected, and public - across multiple packages and observing the results. The key steps are to create classes in one package and call them from another package with different access modifiers. Observations show which access levels allow access within and across classes and packages. The learning outcomes are how to use access modifiers with packages in Java and understand the requirements of each access modifier.

Uploaded by

prerna
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PRACTICAL- 1.

Name : Abhijeet Kumar Pandit


UID : 20BCS2049
Branch BE-CSE
Subject : JAVA Lab
Semester : 3rd
Date: 2-10-2021
Section: CSE8-A

1. Aim / Overview of the practical: Write program to study


access modifiers in java using packages.

2. Types of Access Modifiers in Java:


 Default
 Private
 Protected
 Public

3. Algorithm / Flowchart :

o Start
o Create different packages.
o Create only class in package 1.
o Create main class with main method in package 2.
o Create object of class of package 1 in the main method of
main class of package 2.
o Repeat step 1 to 5 for different access modifiers.
o Study the difference of using different access modifiers.
o Stop.
4. Default Access Modifier :

 Code :
 Package 1 :

package package_1;

class JavaStudent {
void display() {
System.out.println("Class of package 1");
}
}

 Package 2 :

package package_2;
import package_1.*;
public class Main {
public static void main(String[] args) {
JavaStudent obj = new JavaStudent();
obj.display();
}
}
 Output :

5. Private Access Modifier :

 Code :
 Package 1 :

package package_1;

public class JavaStudent {


private void display() {
System.out.println("Class of package 1");
}
}

 Package 2 :

package package_2;
import package_1.*;
public class Main {
public static void main(String[] args) {
JavaStudent obj = new JavaStudent();
obj.display();
}
}
 Output :

6. Protected Access Modifier :

 Code :
 Package 1 :
package package_1;

public class JavaStudent {


protected void display() {
System.out.println("Class of package 1");
}
}
 Package 2 :

package package_2;
import package_1.*;
public class Main {
public static void main(String[] args) {
JavaStudent obj = new JavaStudent();
obj.display();
}
}
 Output :

7. Public Access Modifier :

 Code :
 Package 1 :
package package_1;

public class JavaStudent {


public void display() {
System.out.println("Class of package 1");
}
}
 Package 2 :
package package_2;
import package_1.*;
public class Main {
public static void main(String[] args) {
JavaStudent obj = new JavaStudent();
obj.display();
}
}

 Output :

8. Observations/ Discussions/ Complexity Analysis:


Access Modifier Within class Within package Outside package by Outside package
subclass only
Private Y N N N
Default Y Y N N
Protected Y Y Y N
Public Y Y Y Y

Learning outcomes ( What I have learnt ):


1. How we can use access modifiers using packages in java.

2. What are the uses and requirements of different access modifiers.

3. Creation of classes.

4. Creation of different access modifiers.

5. Use of different access modifiers.

6. Applications of access modifiers.

7. Creation and use of packages.

You might also like