Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

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)

  • identifies interfaces implemented by the class
  • consists of the implements keyword followed by a comma seperated list of the names of the interfaces to be implemented
            class X implements interfaceA, interfaceB, ... { }
    
  • a class must provide a method implementation (execution code) for every method declared in or inherited by the interface
  • if an interface is not provided in the implements clause, the class does not implement the interface even if it provides an implementation for every method declared in the interface

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


Source Package Import Class Interface Constructors
  Methods main() Identifiers Keywords Defaults Arrays
  Primitives # Literals char Literal String Literals Class Literals