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.
What may an event/listener system be used for in Java?
Events and listeners are used to solve a problem where your program will have to wait for some thing to happen.
What is event-driven programming?
Event-driven programming entails having an action wait for an event to happen. Instead of checking at intervals and wasting memory, we release our resources, and let the program tell it when an event happens.
Where may you use an event/listener?
A GUI, where you press a button and something happens. In this case, the event of a button being pressed would call an action or method.
What is the delegation model in Java?
Upon a change in the internal state of an event source, an event is generated, which is an object that typically contains information about said state change. A listener, that has been registered to the event, is then notified of that event happening.
How do you register a listener in Java?
An event source, such as a button, will have an addCategoryListener() method, where Category may be an Action, a Key, etc.
An object that extends CategoryListener may then register itself (or have itself registered) using that method. When that event is called, it will then activate the categoryPerformed(ActionEvent e) method, where CategoryEvent is our distributed Event object.
How may we read the contents of an Event object?
When the Event source generates an Event object, it will get passed in as an argument to the categoryPerformed method. We can then read the contents.
How could we create our own Event system?
We need…
..a Listener class that extends EventListener
..a corresponding Event class that extends Event
..and a Listener instance that extends your created Listener class.
What are generics?
Generics allow algorithms to be written for types that may be specified at a later point. They let you reuse the same code with different input types.
How may we declare a generic type in a class?
If the class has an identity of Class<T>, then we may choose for one of our variable types to simply be T variable.</T>