Advanced Development in Java Flashcards
What is a nested class?
A nested class is one where we put the class definition of a class inside the definition of another class.
Can a class nested inside another access its private variables?
Yes, any class nested within another may access any of its functionalities. The nested class is simply there for the sake of abstraction.
Why may you need to use a nested class?
You typically want classes to be specialised to a certain task. When you have two classes that rely heavily on each other, it can get annoying having to declare getters/setters or other access functions every time you want to add a new thing.
Nesting the class allows them to share functionality amongst themselves while also ensuring sound encapsulation.
What is the difference between a static nested class and an inner class?
Static nested classes do not have access to instance members, while inner classes do.
What access modifiers may a nested class have?
A nested class may have any of the standard modifiers, as it is treated like a variable of the outer class it is within.
Can inner classes be accessed from the outside?
Yes, as long as they have the appropriate visibility modifier.
One may be instantiated, for example, in the following form: OuterClass.new InnerClass();
How does the ‘this’ keyword work with inner classes?
‘this’, when cast in the inner class, refers to the inner type instance.
OuterClass.this, then, refers to the instance of the surrounding class.
What is a local class?
A local class is defined, and exists solely within, a block (typically a method).
Can local classes have access modifiers?
They may not, as they may not be accessed from anywhere other than the private scope.
Can local classes access method-scoped variables?
No, unless they are declared as final. The class is its own encapsulated “zone”.
What is an anonymous class?
An anonymous class is a class declared without any class name at all.
How may an anonymous class be instantiated?
Anonymous classes are instantiated by creating an instance of an interface, and filling out the functionality required within curly brackets.
Where may an anonymous class be instantiated?
An anonymous class may be instantiated anywhere with a scope.
What is an enumerated class?
An enumerated class is a data type consisting of a set of named variables with a specific range.
They are not instantiated, and therefore are effectively static and final.
Can enumerated classes be declared locally?
No. They must be declared as a class or a class member, because they are static and final.