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

B. Write A JAVA Program To Implement Inner Class and Demonstrate Its Access Protection.

This Java program demonstrates an inner class and its access protection. The outer class contains an inner class that can access the outer class's data members and methods. The main method creates an instance of the outer class and calls its display method, which instantiates the inner class and prints values from both classes. It also directly instantiates the inner class and calls its inmethod, showing it can also access the outer class's data. The output verifies the classes can access each other as intended.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

B. Write A JAVA Program To Implement Inner Class and Demonstrate Its Access Protection.

This Java program demonstrates an inner class and its access protection. The outer class contains an inner class that can access the outer class's data members and methods. The main method creates an instance of the outer class and calls its display method, which instantiates the inner class and prints values from both classes. It also directly instantiates the inner class and calls its inmethod, showing it can also access the outer class's data. The output verifies the classes can access each other as intended.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

/*b.

Write a JAVA Program to implement Inner class and demonstrate its


Access protection.*/
package pgm1b;
class outer
{
int outdata = 10;
void display()
{
inner inobj = new inner();
System.out.println("Accessing from outer class");
System.out.println("The value of outdata is " +outdata);
System.out.println("The value of indata is " +inobj.indata);
}
class inner
{
int indata = 20;
void inmethod()
{
System.out.println("Accessing from inner class");
System.out.println("The sum of indata & outdata is " +(outdata + indata));
}
}
}
public class InOutClass
{
public static void main(String[] args)
{
outer outobj = new outer();
outobj.display();
outer.inner inobj1 = outobj.new inner();
inobj1.inmethod();
}
}

Output:
Accessing from outer class
The value of outdata is 10
The value of indata is 20
Accessing from inner class
The sum of indata & outdata is 30

You might also like