Inner Classes Parag Shah Adaptive Software Solutions
Agenda What are inner classes Types of inner classes Uses of inner classes
What Are Inner Classes An inner class is a class within a class It has access to certain members of the outer class
Introduction To Inner Classes Defining an Inner Class public class Outer { public class Inner { public someMethod() {} } } The inner class has access to members of the enclosing class The inner class can be instantiated by another class through it's enclosing class' reference When to use an inner class See  [InnerClassesExample.java]
Static Nested Classes Defining an Inner Class public class Outer { public static class Inner { public someMethod() {} } } Can access only static members of the enclosing class Can be instantiated without without a reference to the enclosing class When to use static inner classes
Anonymous Inner Classes Anonymous inner classes do not have a name public class SoccerDataBuilder { public List parse() { Parser p = new Parser(); p.parse(new Ignorable() { public boolean isValid() { return true; } ); } }
Uses of Anonymous Inner Classes Event handlers for UI events The class' main use is to be represented as the interface it implements Ideally have a few lines of code Callbacks See  [AnonymousInnerExample.java]
Local Inner Classes Inner classes can be defined in methods Very rarely used
Inner Classes And Upcasting An inner class can be upcast into an Interface or a super class This is the primary use in anonymous inner classes
Private Inner Classes Inner classes can be private Cannot be instantiated from another class The enclosing class can control their instantiation and return them as an implementing interface See  [PrivateInnerClass.java]
Accessing The Outer Class An inner class can access any attribute or method in the outer class directly An inner class can produce a reference to the outer class by using OuterClassName.this
Summary Creating Inner Classes Types of inner classes Accessing outer class elements from an Inner Class

Inner Classes

  • 1.
    Inner Classes ParagShah Adaptive Software Solutions
  • 2.
    Agenda What areinner classes Types of inner classes Uses of inner classes
  • 3.
    What Are InnerClasses An inner class is a class within a class It has access to certain members of the outer class
  • 4.
    Introduction To InnerClasses Defining an Inner Class public class Outer { public class Inner { public someMethod() {} } } The inner class has access to members of the enclosing class The inner class can be instantiated by another class through it's enclosing class' reference When to use an inner class See [InnerClassesExample.java]
  • 5.
    Static Nested ClassesDefining an Inner Class public class Outer { public static class Inner { public someMethod() {} } } Can access only static members of the enclosing class Can be instantiated without without a reference to the enclosing class When to use static inner classes
  • 6.
    Anonymous Inner ClassesAnonymous inner classes do not have a name public class SoccerDataBuilder { public List parse() { Parser p = new Parser(); p.parse(new Ignorable() { public boolean isValid() { return true; } ); } }
  • 7.
    Uses of AnonymousInner Classes Event handlers for UI events The class' main use is to be represented as the interface it implements Ideally have a few lines of code Callbacks See [AnonymousInnerExample.java]
  • 8.
    Local Inner ClassesInner classes can be defined in methods Very rarely used
  • 9.
    Inner Classes AndUpcasting An inner class can be upcast into an Interface or a super class This is the primary use in anonymous inner classes
  • 10.
    Private Inner ClassesInner classes can be private Cannot be instantiated from another class The enclosing class can control their instantiation and return them as an implementing interface See [PrivateInnerClass.java]
  • 11.
    Accessing The OuterClass An inner class can access any attribute or method in the outer class directly An inner class can produce a reference to the outer class by using OuterClassName.this
  • 12.
    Summary Creating InnerClasses Types of inner classes Accessing outer class elements from an Inner Class