Thinking in Java (Chapter 10) === ###### tags: `Java` `OOP` 1. If you want to make an object of the inner class anywhere except from within a non-static method of the outer class, you must specify the type of that object as OuterClassName.InnerClassName 1. Iterator design pattern 1. This is very different from the design of nested classes in C++, which is simply a namehiding mechanism. There is no link to an enclosing object and no implied permissions in C++ 1. The inner class secretly captures a reference to the particular object of the enclosing class that was responsible for creating it. Then, when you refer to a member of the enclosing class, that reference is used to select that member. 1. if you make a nested class (a static inner class), then it doesn’t need a reference to the outer-class object 1. The private inner class provides a way for the class designer to completely prevent any type-coding dependencies and to completely hide details about implementation. 1. Anonymous inner classes"Create an object of an anonymous class that’s inherited from Contents” return new Contents() { // Insert a class definition 1. If you’re defining an anonymous inner class and want to use an object that’s defined outside the anonymous inner class, the compiler requires that the argument reference be final 1. If you don’t need a connection between the inner-class object and the outerclass object, then you can make the inner class static, commonly called a nested class 1. You can’t have a named constructor in an anonymous class (since there’s no name!), but with instance initialization 1. You can’t overload instance initializers, so you can have only one of these constructors. Anonymous inner classes are somewhat limited compared to regular inheritance, because they can either extend a class or implement an interface, but not both. And if you do implement an interface, you can only implement one. 1. A nested class means: 1. You don’t need an outer-class object in order to create an object of a nested class. 2. You can’t access a non-static outer-class object from an object of a nested class. Roughly similar to nested classes in C++, except that those classes cannot access private members as they can in Java 1. Ordinary inner classes cannot have static data, static fields, or nested classes. However, nested classes can have all of these 2. A nested class can be part of an interface. Any class you put inside an interface is automatically public and static. You can even implement the surrounding interface in the inner class 1. It’s convenient to nest a class inside an interface when you want to create some common code to be used with all different implementations of that interface. 1. suggested putting a main( ) in every class to act as a test bed .One drawback to this is the amount of extra compiled code you must carry around. If this is a problem, you can use a nested class to hold your test code 1. Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation 1. Inner classes effectively allow you to inherit from more than one non-interface. 1. A closure is a callable object that retains information from the scope in which it was created 1. One of the most compelling arguments made to include some kind of pointer mechanism in Java was to allow callbacks 1. An application framework is a class or a set of classes that’s designed to solve a particular type of problem 1. The Template Method contains the basic structure of the algorithm, and it calls one or more overrideable methods to complete the action of that algorithm. 1. A system that primarily responds to events is called an event-driven system. A common problem in application programming is the graphical user interface (GUI) 1. That information is supplied during inheritance, when the action( ) portion of the algorithm is implemented. 2. Command design pattern—each object in eventList is a request encapsulated as an object 3. it’s more flexible to read the events from a file instead of hardcoding them 1. Anonymous inner classes"Create an object of an anonymous class that’s inherited from Contents” return new Contents() { // Insert a class definition 1. If you’re defining an anonymous inner class and want to use an object that’s defined outside the anonymous inner class, the compiler requires that the argument reference be final 1. To inherit from Inner class, you can’t just pass a reference to an enclosing object. In addition, you must use the syntax: enclosingClassReference.super(); 1. there isn’t any extra inner-class magic going on when you inherit from the outer class. The two inner classes are completely separate entities, each in its own namespace. However, it’s still possible to explicitly inherit from the inner class 1. A local inner class cannot have an access specifier because it isn’t part of the outer class, but it does have access to the final variables in the current code block and all the members of the enclosing class 1. Reason to use local inner class instead of anonymous class: 1. if you need a named constructor and/or an overloaded constructor, since an anonymous inner class can only use instance initialization. Or if you need to make more than one object of that class. 1. If inner classes are anonymous, the compiler simply starts generating numbers as inner-class identifiers. If inner classes are nested within inner classes, their names are simply appended after a ‘$’ and the outer-class identifier (s). 1. Interfaces and inner classes solve the same problem that C++ attempts to solve with its multiple inheritance (MI) feature. However, MI in C++ turns out to be rather difficult to use 1. the fixed-sized constraint of an array is too limiting