Mapa mental
Inner classes
Contenido del mapa
Inner classes
"Regular" Inner Class: inner class that is not Static, Method-local, Anonymous
- An inner class instance has access to all members of the outer class, even those marked private, as if the inner class were part of the outer class
- If you wrote the outer class, then you also wrote the inner class! So you're not violating encapsulation; you designed it this way
- Simulate the forbidden multiple inheritance
- Inner class instance shares a "special relationship" with an instance of the outer class
- The inner class is a part of the outer class. Not just a "part" but a full-fledged, card-carrying member of the outer class.
- class MyOuter { class MyInner { } }
- %javac MyOuter.java MyOuter.class MyOuter$MyInner.class
- You can't say %java MyOuter$MyInner: the inner class file isn't accessible in the usual way
- Can't have static declarations of any kind ecxept static final attributes
- The only way you can access the inner class is through a live instance of the outer class!
- Instantiating an Inner Class
- from Within the Outer Class: MyInner in = new MyInner();
- MyOuter mo = new MyOuter(); MyOuter.MyInner inner = mo.new MyInner();
- The rules for an inner class referencing itself or the outer instance
- Use this to reference the inner class instance itself, from within the inner class code
- Use OuterClass.this To reference the "outer this" (the outer class instance) from within the inner class code
- A regular inner class is a member of the outer class just as instance variables and methods are
- Member Modifiers Applied to Inner Classes
- public private protected No access modifier: default
- final abstract strictfp
- static: but static turns it into a static nested class not an inner class
this keyword
- The keyword this can be used only from within instance code. In other words, not within static code.
- The this reference is a reference to the currently executing object. In other words, the object whose reference was used to invoke the currently running method.
- The this reference is the way an object can pass a reference to itself to some other code, as a method argument
Method-Local Inner Classes
- Can be instantiated only within the method where the inner class is defined below the inner class definition (or the compiler won't be able to find the inner class)
- Like regular inner class objects, the method-local inner class instance shares a special relationship with the enclosing (outer) class instance, and can access its private (or any other) members
- The inner class object cannot use the local variables of the method the inner class is in
- The local variables of the method live on the stack, and exist only for the lifetime of the method but the inner class object lives on the heap (may still alive after the method completes if, for example, a reference to it was passed into some other code and then stored in an instance variable)
- Because the local variables aren't guaranteed to be alive as long as the method-local inner class object, the inner class object can't use them.
- Unless the local variables are marked final
- Compile error: local variable z is accessed from within inner class; needs to be declared final
- Method-Local inner classe declared in a static method
- Has access to only static members of the enclosing class
- There is no this
- Method-Local inner classe in a static method is subject to the same restrictions as the static method
- No access to instance variables
Anonymous Inner Class
- Comes in two flavors from class & from interface
- Popcorn p = new Popcorn() {}; : refers not to an instance of Popcorn, but to an instance of an anonymous (unnamed) subclass of Popcorn
- The whole point is to override one or more methods of the superclass or to implement methods of an interface
- Polymorphism is in play when anonymous inner classes are involved
- Can implement only one interface
- Cannot add public methods other than the overridden ones (Useless)
- Argument-Defined Anonymous Inner Class: declare and instanciate an Anonymous inner class as a parameter value
- Automatically becomes an implementer of any interfaces implemented by the superclass
Static Nested Classes
- They really aren't inner classes at all
- Does not share a special relationship with the outer class
- It is simply a non-inner (also called "top-level") class scoped within another
- it's really more about name-space resolution than about an implicit relationship between the two classes
- simply a class that's a static member of the enclosing class
- does not have access to the instance variables and nonstatic methods of the outer class
- from Within the Outer Class: Nested n = new Nested() Outside the Outer class: Outer.Nested n = new Outer.Nested();
Información
- Descripción
- Mapa Mental sobre Inner classes, creado por Abdelkrim ZAHMOUNI el 04/10/2018.
- Creado por
- Abdelkrim ZAHMOUNI · oct 2018
- Actualizado
- 5 nov 2018
- Visitas
- 0
- Historial
-
- Creado por Abdelkrim ZAHMOUNI · 23 sep 2018
- Copiado por Abdelkrim ZAHMOUNI · 3 oct 2018
- Copiado por Abdelkrim ZAHMOUNI · 3 oct 2018
- Copiado por Abdelkrim ZAHMOUNI · 4 oct 2018
- Valoración