Inner classes Hmmmmm so confusing ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gautamz07
    New Member
    • Sep 2013
    • 26

    Inner classes Hmmmmm so confusing ?

    The below code is a simple inner class and understandable , also of late people do prefer using inner classe more espically Anonymous Inner Classes
    Code:
    public class First    // Top-level class; outer class or enclosing class
    {
      int price = 100;   // variable of outer class
     
      class Second       // inner class as a member of outer class
      {
        int rate = 200;              // variable of inner class
     
        public void display()        // inner class method
        {
          System.out.println("price from display() method: " + price);
          System.out.println("rate from display method: " + rate );					
        }
      }                              // inner class closes
     
      public void show()             // outer class method
      {
        System.out.println("price from show() method: " + price);
        // System.out.println("y from outer method: " + y);  // raises compilation error, scope problem
      }
      public static void main(String args[])    
      {
        First f1 = new First();
        First.Second fs = new First( ).new Second();
        f1.show();
        fs.display();
      }
    }
    Please do help me with Anonymous Inner Classes as its difficult tu understand except for the fact that its not re suable and good for coding on the fly .
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    What exactly is it about anonymous inner classes you're trying to understand? What information or help are you looking for?

    Generally speaking, anonymous inner classes can be great tools for objects you only need in one specific situation. They should be relatively short though and in most cases they should extend another class or implement an interface as otherwise you won't be able to access most of what they can do afterwards. So for example, say you wanted to have your Second class implemented like that it might look something like this:
    [code=java]public class First // Top-level class; outer class or enclosing class
    {
    int price = 100; // variable of outer class

    interface Second // inner interface as a member of outer class
    {
    int rate = 200; // variable of inner interface

    public void display(); // inner interface method

    } // inner interface closes

    public void show() // outer class method
    {
    System.out.prin tln("price from show() method: " + price);
    // System.out.prin tln("y from outer method: " + y); // raises
    // compilation error, scope problem
    }

    public static void main(String args[]) {
    final First f1 = new First(); // Objects which should be accessible in anonymous implementations have to be final
    First.Second fs = new Second() // Anonymous implementation of above interface
    {
    @Override
    public void display() // The implementation of the function mentioned in the interface
    {
    System.out.prin tln("price from display() method: " + f1.price); // Here you access the price field in the final instance of First created before
    System.out.prin tln("rate from display method: " + rate); // This field is internal to the interface and therefore to the anonymous implementation
    }
    };
    f1.show();
    fs.display();
    }
    }[/code] I hope that helps?

    Comment

    • gautamz07
      New Member
      • Sep 2013
      • 26

      #3
      srry for the late reply . Helped alot :) infact i understand the whole programme now . Thank you .


      Gautam

      Comment

      Working...