7. Inner Classes Flashcards
What is a key benefit of an inner class?
One of the key benefits of an inner class is the special relationship an inner class instance shares with an instance of the outer class. That special relationship gives code in the inner class access to members of the outer class.
What is an inner class to an outer class?
The inner class is a part of the outer class. An inner class instance has access to all members of the outer class, even those marked private.
How to acces the inner class?
A regular inner class cannot have static declarations of any kind. The only way you can access the inner class is through a live instance of the outer class. To create an instance of an inner class, you must have an instance of the outer class to tie to the inner class.
How to create an instance of the inner class?
MyOuter.MyInner inner = new MyOuter().new Myinner();
Or if you already have an instance of the outer class:
MyOuter.MyInner inner = outerObjRef.new MyInner();
Where does this refers to in the inner class?
Within the inner class code, the this reference refers to the instance of the inner class. To reference the “outer this” from within the inner class code, use NameOfOuterClass.this (example, MyOuter.this)
Which acces modifiers can be applied to inner classes?
A regular inner class is a member of the outer class just as instance variables and methods are, so the following modifiers can be applied to an inner class: - Final - Abstract - Public - Private - Protected - Static - Strictfp
What is a Method-local inner class?
You can also define an inner class within a method. To use the inner class, you must make an instance of it somewhere within the method but below the inner class definition
How can a method local inner class be instantiated?
A method-local inner class can be instantiated only within the method where the inner class is defined. So no other code running in any other method – inside or outside the class – can ever instantiate the method local inner class.
Can an inner class use the local variables of the method he is in?
the inner class object cannot use the local variables of the method the inner class is in, unless they are marked final or are effectively final
Which acces modifiers can be applied to method local inner classes?
the only modifiers you can apply to a method-local inner class are abstract and final, but never both at the same time.
Can a method local inner class be defined in a static method?
Remember that a local class declared in a static method has access only to static members of the enclosing class, since there is no associated instance of the enclosing class. If you are in a static method, there is no this, so inner class in a static method is subject to the same restrictions as the static method.
What is an anonymous inner class?
Anonymous inner classes are declared without any classname at all. And if thats not weird enough,
you can define these classes, not just within a method, but even within an argument to a method.
What is the point of making an anonymous inner class?
The whole point of making an anonymous inner class, is to override one or more methods of the
superclass.
Which methods can you call in an anonymous inner class?
You can only call methods on an anonymous inner class reference that are defined in the reference variable type. This is no different from any other polymorphic reference.
What are Anonymous interface implementers?
One more thing to keep in mind about anonymous interface implementers: they can implement only one interface. They can’t even extend a class and implement an interface at the same time.