Two-Minute Drill


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.

Identifiers (Objective 1. 3)

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.

Declaration Rules (Objective 1.1)

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.

Class Access Modifiers (Objective 1.1)

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  

Create an instance of another class

q  

Extend (or subclass), another class

q  

Access methods and variables of another class

Class Modifiers (Nonaccess) (Objective 1.2)

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.

Interface Implementation (Objective 1.2)

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  

It provides concrete implementations for the interface's methods.

q  

It must follow all legal override rules for the methods it implements.

q  

It must not declare any new checked exceptions for an implementation method.

q  

It must not declare any checked exceptions that are broader than the exceptions declared in the interface method.

q  

It may declare runtime exceptions on any interface method implementation regardless of the interface declaration.

q  

It must maintain the exact signature (allowing for covariant returns) and return type of the methods it implements (but does not have to declare the exceptions of the interface).

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.

Member Access Modifiers (Objectives 1.3 and 1.4)

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  

Code in one class can access a member of another class.

q  

A subclass can inherit a member of its superclass.

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  

Default members can be accessed only by classes in the same package.

q  

protected members can be accessed by other classes in the same package, plus subclasses regardless of package.

q  

protected = package plus kids (kids meaning subclasses).

q  

For subclasses outside the package, the protected member can be accessed only through inheritance; a subclass outside the package cannot access a protected member by using a reference to a superclass instance (in other words, inheritance is the only mechanism for a subclass outside the package to access a protected member of its superclass).

q  

A protected member inherited by a subclass from another package is not accessible to any other class in the subclass package, except for the subclass' own subclasses.

Local Variables (Objective 1.3)

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.

Other Modifiers—Members (Objective 1.3)

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 method is not marked abstract.

q  

The method has curly braces.

q  

The method has code between the curly braces.

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  

abstract methods cannot be private.

q  

abstract methods cannot be final.

q  

The native modifier applies only to methods.

q  

The strictfp modifier applies only to classes and methods.

Methods with Var-args (Objective 1.4)

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.

Variable Declarations (Objective 1.3)

q  

Instance variables can

q  

Have any access control

q  

Be marked final or transient

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  

final variables cannot be reinitialized once assigned a value.

q  

final reference variables cannot refer to a different object once the object has been assigned to the final variable.

q  

final reference variables must be initialized before the constructor completes.

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.

Array Declarations (Objective 1.3)

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.

Static Variables and Methods (Objective 1.4)

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.

Enums (Objective 1.3)

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}; 




SCJP Sun Certified Programmer for Java 5 Study Guide Exam 310-055
SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055) (Certification Press)
ISBN: 0072253606
EAN: 2147483647
Year: 2006
Pages: 131

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net