Module 02 - Advanced Class Design
Module 02 - Advanced Class Design
John Yeary
Module
2
-
Objectives
Identify
when
and
how
to
apply
abstract
classes.
Construct
abstract
Java
classes
and
subclasses.
Use
the
static
and
nal
keywords.
Create
top-level
and
nested
classes.
Use
enumerated
types.
Abstract
Classes
A
class
declared
as
abstract
can
not
be
instantiated,
and
must
be sub-classed. as abstract.
A class is must be declared abstract if any method is declared Fields declared in an abstract class are not public, static, or
be declared as an interface.
An enum can not be declared as abstract. An abstract class is used to encapsulate elds and behaviors
nal
modier
nal
can
be
applied
to
classes,
elds,
and
methods.8.1.1.2,
A nal class can not be sub-classed. All methods of a nal class are nal, and can not be
overridden.
nal
modier
A
method
can
be
declared
nal
to
prevent
sub-classes
from
body of a nal method, replacing an invocation of the method with the code in its body. The inlining process must preserve the semantics of the method invocation. 8.4.3.3
Local eld declarations can be nal. A resource of a try-with-resources statement (14.20.3) and an
exception parameter of a multi-catch clause (14.20) are implicitly declared nal. 4.12.4
static
modier
A
static
class
modier
can
be
only
applied
to
member
classes
A static eld is incarnated when the class in initialized A static method is also called a class method. A static method can be invoked without reference to a
constant.9.3
compile time. It is a compile time constant. The compiler replaces all instances with the actual value.
Class
Declaration
A
public
class
must
be
in
a
source
le
which
has
the
same
name
as
The source le does not need to be named for any of the classes import and package declarations apply to all classes within the
source le.
before any imports, or class declarations. and before the class declaration.
If there are any imports, they must occur after the package name, A class may be declared inside of another class (enclosing class).
There are two types of nested (inner-classes): static, and instance A class may created as an anonymous inner-class by directly
implementing an interface.
enum
types
An enum can not be abstract.
8.9 An enumerated type can not be cloned.
8.9 It is a compile time error to explicitly instantiate an enum.
15.9.1
An enum contains constants which form the body of the
enumeration.
8.9.1
You can use == or equals to determine equality since
private
constructor
that
takes
no
parameters
(to
match
the
implicit
empty
argument
list)
is
automatically
provided.
8.9.
An
enum
constant
is
implicitly
static
and
nal.
16.5
A
nested
enum
is
implicitly
static.
8.9
An
enum
may
not
be
local,
nor
can
it
be
inside
an
inner
class.
statement.
enum
example
public
enum
Coin
{
PENNY(1),
NICKEL(5),
DIME(10),
QUARTER(25);
Coin(int
value)
{
this.value
=
value;
}
private
nal
int
value;
public
int
value()
{
return
value;
}
}
References
Java
Language
Specication,
Java
SE
7
Edition,
James
Gosling,
Preparation for Java Programmer Language Certication Sun Certied Java Programmer for Java 6 Study Guide, Kathy