Unit 8 (Prog 1) : Test Inner Class

By | June 11, 2013

In this example, you will find the concept of Inner class in java, how to declare and access the inner class properties.

Access modifiers and their effect on inner class properties.

Java doc for Inner class in given Here : Inner class in java

 class Outer{
    private int i = 10;
    class Inner{
       public String println(){
          return "Accessing private member of outer class from Inner class : " + i;
       }
   }

   // Accessing Inner class method from Outer class
   void fromOuterClass(){
      new Inner().println();
   }

   public String println(){
      return "Accessing private member of outer class : " + i;
   }

   // overriding toString() method
   public String toString(){
      return "Accessing private member of outer class : " + i;
   }
}

public class TestInnerClass {
   // Accessing Inner class method from outside Outer class.
   public static void main(String a[]){
      Outer out = new Outer();
      out.fromOuterClass();
      Outer.Inner in = out.new Inner();
      System.out.println(in.println());
      System.out.println(out.toString());
   }
}
email

Leave a Reply

Your email address will not be published. Required fields are marked *