Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

Access Modifiers

By:
Prof. Monika Gadre
Prof. Monika Gadre
Prof. Monika Gadre
Prof. Monika Gadre
Question 1
 Trace the output
private class AA
{
public int a=10;
}
A class can only be declared as public or default.
Private and protected are not allowed for a class
definition.

Prof. Monika Gadre


Question 2
 Trace the output
package A;
class AA class AAA{
{ public static void main(String a[]){
public int x=10; System.out.println(x);
int y=20; System.out.println(y);
private int z=30; System.out.println(z);
protected int w=40; System.out.println(w);
} }
 Data member z is private and is not accessible in another class even though it belongs to the
same package

Prof. Monika Gadre


Question 3
 Trace the output
package A; package B;
import A.AA;
class AA class AAA{
{ public static void main(String a[]){
AA obj = new AA();
public int x=10; System.out.println(obj.x);
int y=20; System.out.println(obj.y);
private int z=30; System.out.println(obj.z);
protected int w=40; System.out.println(obj.w);
} }

Output: AccessModifiersExample.java:7: error: AA is not public in A; cannot be accessed from


outside package AA
obj = new AA();
Prof. Monika Gadre

^
Question 4
 Trace the output
package A; package B;
import A.AA;
public class AA class AAA{
{ public static void main(String a[]){
public int x=10; System.out.println(x);
int y=20; System.out.println(y);
private int z=30; System.out.println(z);
protected int w=40; System.out.println(w);
} }

Output: only public data member i.e., x is accessible in the class AAA that is another package.
Whereas y, z, w are not accessible though the class AA is public.
Prof. Monika Gadre

You might also like