Thinking in Java (Chapter 7) === ###### tags: `Java` `OOP` 1. Because you can only “add” a String to another String. Any time you want to allow this behavior with a class you create, you need only write a toString( ) method 1. If you want the references initialized(1): At the point the objects are defined. This means that they’ll always be initialized before the constructor is called(2) In the constructor for that class(3) Right before you actually need to use the object. This is often called lazy initialization(4) Using instance initialization 1. Even if a class has package access, a public main() is accessible. 1. as a general rule make all fields private and all methods public 1. super that refers to the “superclass” that the current class inherits 1. When you create an object of the derived class, it contains within it a subobject of the base class 2. If you don’t call the base-class constructor in the current constructor if it’s not default constructor(it has argument), the compiler will complain that it can’t find a constructor of that form. In addition, the call to the base-class constructor must be the first thing you do in the derived-class constructor. 1. The constructor doesn’t watch over you to make sure that you initialize the member objects, so you must remember to pay attention to that 1. The try keyword indicates that the block that follows (delimited by curly braces) is a guarded region 1. The code in the finally clause following this guarded region is always executed, no matter how the try block exits 1. First perform all of the cleanup work specific to your class, in the reverse order of creation. Then call the base-class cleanup method 1. You can’t rely on garbage collection for anything but memory reclamation. If you want cleanup to take place, make your own cleanup methods and don’t use on finalize( ). 1. You can see that all the overloaded methods of Homer are available in Bart, even though Bart introduces a new overloaded method (in C++ doing this would hide the base-class methods). 1. Java SE5 has added the @Override annotation, which is not a keyword but can be used as if it were. When you mean to override a method, you can choose to add this annotation and the compiler will produce an error message if you accidentally overload instead of overriding. 1. Composition is generally used when you want the features of an existing class inside your new class, but not its interface. You embed private objects of existing classes inside your new class. 1. Making the members public assists the client programmer’s understanding of how to use the class and requires less code complexity for the creator of the class, but in general you shall make fields private 1. The protected keyword is a nod to pragmatism. It says “This is private as far as the class user is concerned, but available to anyone who inherits from this class or anyone else in the same package.” (In Java, protected also provides package access.) 2. the act of converting the reference of derived class into the reference of base class is called upcasting 1. The only thing that can occur to the class interface during the upcast is that it can lose methods, not gain them. This is why the compiler allows upcasting without any explicit casts or other special notation 1. If you must upcast, then inheritance is necessary 1. In Java, constants that you don’t want to change in runtime must be primitives and are expressed with the final keyword. A value must be given at the time of definition of such a constant. 1. A field that is both static and final has only one piece of storage that cannot be changed. 1. With an object reference, final makes the reference a constant. Once the reference is initialized to an object, it can never be changed to point to another object. However, the object itself can be modified 1. By convention, fields that are both static and final (that is, compile-time constants) are capitalized and use underscores to separate words. 1. Just because something is final doesn’t mean that its value is known at compile time 1. Final means that you cannot rebind the reference to a new object. You can also see that the same meaning holds true for an array, which is just another kind of reference 1. The blank final must be initialized before it is used 1. You’re forced to perform assignments to finals either with an expression at the point of definition of the field or in every constructor. That way it’s guaranteed that the final field is always initialized before use. 1. Make arguments final by declaring them as such in the argument list. This means that inside the method you cannot change what the argument reference points to 1. reason for final method: make sure that a method’s behavior is retained during inheritance and cannot be overridden 1. Any private methods in a class are implicitly final. The confusion is that if you try to override a private method (which is implicitly final), it seems to work 1. If a method is private, it isn’t part of the base-class interface. It is just some code that’s hidden away inside the class, and it just happens to have that name 1. When you say that an entire class is final (by preceding its definition with the final keyword), you state that you don’t want to inherit from this class or allow anyone else to do so 1. The fields of a final class can be final or not, as you choose, because it prevents inheritance, all methods in a final class are implicitly final 1. the modern Java container library replaces Hashtable with HashMap 1. the modern Java container library replaces Vector with ArrayList 1. C++ has problems if one static expects another static to be valid before the second one has been initialized 1. “class code is loaded at the point of first use.” This is usually when the first object of that class is constructed, but loading also occurs when a static field or static method is accessed. 1. The statics are initialized only once. 1. In Java, file isn’t loaded until the code is needed. 1. Static initialization in the base class happens->Static initialization in derived class. Object can be created->all value and reference set to default->base class constructor->current class constructor 1. 1. ?After the base-class constructor completes, the instance variables are initialized in textual order. Finally, the rest of the body of the constructor is executed. 1. ?by using the added artifice of inheritance with your member type, you can change the exact type, and thus the behavior, of those member objects at run time. Therefore, you can change the behavior of the composed object at run time