Saturday, September 17, 2016

1 b. Inner class and demonstrate its Access protection

         /* Write a JAVA Program to implement Inner class and demonstrate its      Access protection.*/
          

          //AccessMain.java
          import java.io.*;
          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));
         }
         }
         }
                 
        class  AccessMain
       {
        public  static  void  main(String  args[])
        {
          outer  outobj  = new  outer();
          outobj.display();
          outer.inner  inobj1 = outobj.new  inner();
          inobj1.inmethod();
         }
        }

3 comments:

  1. Really good site, has very good piece of information at one place

    ReplyDelete
  2. Add "public" before "class AccessMain" to run the code properly.

    ReplyDelete