Your SlideShare is downloading. ×
0
Inner Classes Parag Shah Adaptive Software Solutions
Agenda <ul><li>What are inner classes </li></ul><ul><li>Types of inner classes </li></ul><ul><li>Uses of inner classes </l...
What Are Inner Classes <ul><li>An inner class is a class within a class </li></ul><ul><li>It has access to certain members...
Introduction To Inner Classes <ul><li>Defining an Inner Class </li></ul><ul><ul><li>public class Outer { </li></ul></ul><u...
Static Nested Classes <ul><li>Defining an Inner Class </li></ul><ul><ul><li>public class Outer { </li></ul></ul><ul><ul><u...
Anonymous Inner Classes <ul><li>Anonymous inner classes do not have a name </li></ul><ul><li>public class SoccerDataBuilde...
Uses of Anonymous Inner Classes <ul><li>Event handlers for UI events </li></ul><ul><li>The class' main use is to be repres...
Local Inner Classes <ul><li>Inner classes can be defined in methods </li></ul><ul><li>Very rarely used </li></ul>
Inner Classes And Upcasting <ul><li>An inner class can be upcast into an Interface or a super class </li></ul><ul><li>This...
Private Inner Classes <ul><li>Inner classes can be private </li></ul><ul><li>Cannot be instantiated from another class </l...
Accessing The Outer Class <ul><li>An inner class can access any attribute or method in the outer class directly </li></ul>...
Summary <ul><li>Creating Inner Classes </li></ul><ul><li>Types of inner classes </li></ul><ul><li>Accessing outer class el...
Upcoming SlideShare
Loading in...5
×

Inner Classes

1,933

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,933
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
60
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Inner Classes"

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

    Clipping is a handy way to collect important slides you want to go back to later.

×