Remember that in this chapter, when we talk about classes, we're referring to non-inner classes, or top-level classes. We'll devote all of Chapter 8 to inner classes.
q | Identifiers can begin with a letter, an underscore, or a currency character. |
q | After the first character, identifiers can also include digits. |
q | Identifiers can be of any length. |
q | JavaBeans methods must be named using camelCase, and depending on the method's purpose, must start with set, get, is, add, or remove. |
q | A source code file can have only one public class. |
q | If the source file contains a public class, the filename must match the public class name. |
q | A file can have only one package statement, but multiple imports. |
q | The package statement (if any) must be the first (non-comment) line in a source file. |
q | The import statements (if any) must come after the package and before the class declaration. |
q | If there is no package statement, import statements must be the first (non-comment) statements in the source file. |
q | package and import statements apply to all classes in the file. |
q | A file can have more than one nonpublic class. |
q | Files with no public classes have no naming restrictions. |
q | There are three access modifiers: public, protected, and private. | ||||||
q | There are four access levels: public, protected, default, and private. | ||||||
q | Classes can have only public or default access. | ||||||
q | A class with default access can be seen only by classes within the same package. | ||||||
q | A class with public access can be seen by all classes from all packages. | ||||||
q | Class visibility revolves around whether code in one class can
|
q | Classes can also be modified with final, abstract, or strictfp. |
q | A class cannot be both final and abstract. |
q | A final class cannot be subclassed. |
q | An abstract class cannot be instantiated. |
q | A single abstract method in a class means the whole class must be abstract. |
q | An abstract class can have both abstract and nonabstract methods. |
q | The first concrete class to extend an abstract class must implement all of its abstract methods. |
q | Interfaces are contracts for what a class can do, but they say nothing about the way in which the class must do it. | ||||||||||||
q | Interfaces can be implemented by any class, from any inheritance tree. | ||||||||||||
q | An interface is like a 100-percent abstract class, and is implicitly abstract whether you type the abstract modifier in the declaration or not. | ||||||||||||
q | An interface can have only abstract methods, no concrete methods allowed. | ||||||||||||
q | Interface methods are by default public and abstract—explicit declaration of these modifiers is optional. | ||||||||||||
q | Interfaces can have constants, which are always implicitly public, static, and final. | ||||||||||||
q | Interface constant declarations of public, static, and final are optional in any combination. | ||||||||||||
q | A legal nonabstract implementing class has the following properties:
| ||||||||||||
q | A class implementing an interface can itself be abstract. | ||||||||||||
q | An abstract, implementing class docs not have to implement the interface methods (but the first concrete subclass must). | ||||||||||||
q | A class can extend only one class (no multiple inheritance), but it can implement many interfaces. | ||||||||||||
q | Interfaces can extend one or more other interfaces. | ||||||||||||
q | Interfaces cannot extend a class, or implement a class or interface. | ||||||||||||
q | When taking the exam, verify that interface and class declarations are legal before verifying other code logic. |
q | Methods and instance (nonlocal) variables are known as "members. " | ||||||||||
q | Members can use all four access levels: public, protected, default, private. | ||||||||||
q | Member access comes in two forms:
| ||||||||||
q | If a class cannot be accessed, its members cannot be accessed. | ||||||||||
q | Determine class visibility before determining member visibility. | ||||||||||
q | public members can be accessed by all other classes, even in other packages. | ||||||||||
q | If a superclass member is public, the subclass inherits it—regardless of package. | ||||||||||
q | Members accessed without the dot operator (.) must belong to the same class. | ||||||||||
q | this. always refers to the currently executing object. | ||||||||||
q | this.aMethod() is the same as just invoking aMethod(). | ||||||||||
q | private members can be accessed only by code in the same class. | ||||||||||
q | private members are not visible to subclasses, so private members can not be inherited. | ||||||||||
q | Default and protected members differ only when subclasses are involved:
|
q | Local (method, automatic, or stack) variable declarations cannot have access modifiers. |
q | final is the only modifier available to local variables. |
q | Local variables don't get default values, so they must be initialized before use. |
q | final methods cannot be overridden in a subclass. | ||||||
q | abstract methods are declared, with a signature, a return type, and an optional throws clause, but are not implemented. | ||||||
q | abstract methods end in a semicolon—no curly braces. | ||||||
q | Three ways to spot a non-abstract method:
| ||||||
q | The first nonabstract (concrete) class to extend an abstract class must implement all of the abstract class' abstract methods. | ||||||
q | The synchronized modifier applies only to methods and code blocks. | ||||||
q | synchronized methods can have any access control and can also be marked final. | ||||||
q | abstract methods must be implemented by a subclass, so they must be inheritable. For that reason:
| ||||||
q | The native modifier applies only to methods. | ||||||
q | The strictfp modifier applies only to classes and methods. |
q | As of Java 5, methods can declare a parameter that accepts from zero to many arguments, a so-called var-arg method. |
q | A var-arg parameter is declared with the syntax type... name; for instance: doStuff(int... x) { } |
q | A var-arg method can have only one var-arg parameter. |
q | In methods with normal parameters and a var-arg, the var-arg must come last. |
q | Instance variables can
| ||||||
q | Instance variables can't be abstract, synchronized, native, or strictfp. | ||||||
q | It is legal to declare a local variable with the same name as an instance variable; this is called "shadowing." | ||||||
q | final variables have the following properties:
| ||||||
q | There is no such thing as a final object. An object reference marked final does not mean the object itself is immutable. | ||||||
q | The transient modifier applies only to instance variables. | ||||||
q | The volatile modifier applies only to instance variables. |
q | Arrays can hold primitives or objects, but the array itself is always an object. |
q | When you declare an array, the brackets can be to the left or right of the variable name. |
q | It is never legal to include the size of an array in the declaration. |
q | An array of objects can hold any object that passes the IS-A (or instanceof) test for the declared type of the array. For example, if Horse extends Animal, then a Horse object: can go into an Animal array. |
q | They are not tied to any particular instance of a class. |
q | No classes instances are needed in order to use static members of the class. |
q | There is only one copy of a static variable / class and all instances share it. |
q | static methods do not have direct access to non-static members. |
q | An enum specifies a list of constant values that can be assigned to a particular type. |
q | An enum is NOT a String or an int; an enum constant's type is the enum type. For example, WINTER, SPRING, SUMMER, and FALL are of the enum type Season. |
q | An enum can be declared outside or inside a class, but NOT in a method. |
q | An enum declared outside a class must NOT be marked static, final, abstract, protected, or private. |
q | Enums can contain constructors, methods, variables, and constant class bodies. |
q | enum constants can send arguments to the enum constructor, using the syntax BIG(8), where the int literal 8 is passed to the enum constructor. |
q | enum constructors can have arguments, and can be overloaded. |
q | enum constructors can NEVER be invoked directly in code. They are always called automatically when an enum is initialized. |
q | The semicolon at the end of an enum declaration is optional. These are legal:
enum Foo { ONE, TWO, THREE} enum Foo { ONE, TWO, THREE}; |