0% found this document useful (0 votes)
8 views4 pages

Java Inner and Outer Class Access

Uploaded by

vaibhav11rahejaa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Java Inner and Outer Class Access

Uploaded by

vaibhav11rahejaa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA LAB

Vaibhav Raheja

124103021

1. Create a class with a private field and private method. Create an inner class with a
method that modifies the outer class and calls the outer class method. In a second
outer-class method , create an object of the inner class and call its method, then show
the eect on the outer class object.
2. package BASICS;
class OuterClass
{
private int data=10;

private void displayData()


{
[Link]("outer class private data :" + data);
}

class InnerClass
{
void modifyOuterClass()
{
data=50; // inner class outer class ke private fields ko access kr skti hai

[Link]("modified data :"); displayData();


}

} void testInner()
{
InnerClass inner=new InnerClass(); [Link]();
}
public static void main(String[] args) {

OuterClass outer=new OuterClass();


[Link]("before modification : ");

[Link]();

[Link](" calling inner class method through outer class :");

[Link]();

}
}

output :-
2. determine if an outer class has access to private elements of inner class .

// modifying the above code

package BASICS;

class OuterClass
{ private int data = 10;

private void displayData() {


[Link]("Outer class private data: " + data);
}

class InnerClass {
private int innerData = 100; // private member of inner class

private void innerDisplay() {


[Link]("Inner class private data: " + innerData);
}
}

void testInnerAccess() {
InnerClass inner = new InnerClass();

// Accessing private variable of InnerClass


[Link]("Accessing inner private data from Outer: " + [Link]);

// Accessing private method of InnerClass


[Link]();
}

public static void main(String[] args)


{ OuterClass outer = new OuterClass();

[Link]("Before modification: ");


[Link]();

[Link]("\nAccessing inner class private members:");


[Link]();
}
}

output :-

yes it has full access !

You might also like