|
|
Language Fundamentals - Class Declarations
Syntax (JJ pg 137)
modifiers class ClassName extendsClause implementsClause {
// Class body
}
The modifiers, extendsClause and implementsClause are all optional.
Modifiers
public protected private
abstract static final strictfp
- if two or more modifiers are used in a declaration it is customary, but not required, to show them in the order given (JLS 8.1.1)
- no modifiers are allowed in Anonymous class declarations (JJ pg 147)
- A class may not be both final and abstract as an abstract class implies extension
- package access (no access modifier declared) is also referred to as friendly access
- a compile error occurs if the same modifier appears more than once in a declaration (JLS §8.1.1)
extendsClause (JJ pg 137)
- consists of the extends keyword followed by the name of the class being extended
- the extended class is referred to as the parent or superclass
- multiple extends are illegal ie a class may have only one superclass
- if no extends clause is used, the class automatically inherits from the java.lang.Object class
- a compile error occurs if a final class appears in the extends clause (JLS §8.1.1.2)
- an Anonymous class cannot have an extends clause (JPL pg74)
implementsClause (JJ pg 137)
Class body (JJ pg 138)
- the class body declares members (field variables and methods), constructors and initializers
- class members may also be inner classes or interfaces
Traps
- class attempting to extend more than one other class
- class declared both final and abstract
|