Think In JAVA (the third edition) [7]
Chapter 4: Initialization & Cleanup
(Corresponding lecture on the CD ROM). This chapter begins by introducing the constructor, which guarantees proper initialization. The definition of the constructor leads into the concept of method overloading (since you might want several constructors). This is followed by a discussion of the process of cleanup, which is not always as simple as it seems. Normally, you just drop an object when you’re done with it, and the garbage collector eventually comes along and releases the memory. This portion explores the garbage collector and some of its idiosyncrasies. The chapter concludes with a closer look at how things are initialized: automatic member initialization, specifying member initialization, the order of initialization, static initialization, and array initialization.
Chapter 5: Hiding the Implementation
(Corresponding lecture on the CD ROM). This chapter covers the way that code is packaged together, and why some parts of a library are exposed while other parts are hidden. It begins by looking at the package and import keywords, that perform file-level packaging and allow you to build libraries of classes. It then examines the subject of directory paths and file names. The remainder of the chapter looks at the public, private, and protected keywords, the concept of package access, and what the different levels of access control mean when used in various contexts.
Chapter 6: Reusing Classes
(Corresponding lecture on the CD ROM). The simplest way to reuse a class is to embed an object inside your new class with composition. However, composition isn’t the only way to make new classes from existing ones. The concept of inheritance is standard in virtually all OOP languages. It’s a way to take an existing class and add to its functionality (as well as change it—the subject of Chapter 7). Inheritance is often a way to reuse code by leaving the “base class” the same and just patching things here and there to produce what you want. In this chapter you’ll learn how composition and inheritance reuse code in Java, and how to apply them.
Chapter 7: Polymorphism
(Corresponding lecture on the CD ROM). On your own, you might take nine months to discover and understand polymorphism, a cornerstone of OOP. Through small, simple examples, you’ll see how to create a family of types with inheritance and manipulate objects in that family through their common base class. Java’s polymorphism allows you to treat all objects in this family generically, which means that the bulk of your code doesn’t rely on specific type information. This makes your code more flexible, so building programs and code maintenance is easier and cheaper.
Chapter 8: Interfaces & Inner Classes
Java provides special tool to set up design and reuse relationships: the interface, which is a pure abstraction of the interface of an object. The interface is more than just an abstract class taken to the extreme, since it allows you to perform a variation on C++’s “multiple inheritance” by creating a class that can be upcast to more than one base type.
At first, inner classes look like a simple code-hiding mechanism; you place classes inside other classes. You’ll learn, however, that the inner class does more than that; it knows about and can communicate with the surrounding class. The kind of code you can write with inner classes is more elegant and clear. However, it is a new concept to most, and it takes some time to become comfortable with design using inner classes.
Chapter 9: Error Handling with Exceptions
The basic philosophy of Java is that badly-formed code will not be run. As much as possible, the compiler catches problems, but sometimes a problem—either a programmer error or a natural error