Java Reference
In-Depth Information
• Constructors and
static
methods cannot be declared
abstract
.
• Abstract superclass variables can hold references to objects of any concrete class derived from the
superclass. Programs typically use such variables to manipulate subclass objects polymorphically.
• Polymorphism is particularly effective for implementing layered software systems.
Section 10.5 Case Study: Payroll System Using Polymorphism
• A hierarchy designer can demand that each concrete subclass provide an appropriate method im-
plementation by including an
abstract
method in a superclass.
• Most method calls are resolved at execution time, based on the type of the object being manip-
ulated. This process is known as dynamic binding (p. 417) or late binding.
• A superclass variable can be used to invoke only methods declared in the superclass.
•Operator
instanceof
(p. 417) determines if an object has the
is-a
relationship with a specific type.
• Every object in Java knows its own class and can access it through
Object
method
getClass
(p. 418), which returns an object of type
Class
(package
java.lang
).
•The
is-a
relationship applies only between the subclass and its superclasses, not vice versa.
Section 10.7
final
Methods and Classes
• A method that's declared
final
(p. 419) in a superclass cannot be overridden in a subclass.
• Methods declared
private
are implicitly
final
, because you can't override them in a subclass.
• Methods that are declared
static
are implicitly
final
.
•A
final
method's declaration can never change, so all subclasses use the same implementation, and
calls to
final
methods are resolved at compile time—this is known as static binding (p. 420).
• The compiler can optimize programs by removing calls to
final
methods and inlining their ex-
panded code at each method-call location.
• A class that's declared
final
cannot be extended (p. 420).
• All methods in a
final
class are implicitly
final
.
Section 10.9 Creating and Using Interfaces
• An interface (p. 421) specifies
what
operations are allowed but not
how
they're performed.
• A Java interface describes a set of methods that can be called on an object.
• An interface declaration begins with the keyword
interface
(p. 421).
• All interface members must be
public
, and interfaces may not specify any implementation de-
tails, such as concrete method declarations and instance variables.
• All methods declared in an interface are implicitly
public
abstract
methods and all fields are
implicitly
public
,
static
and
final
.
• To use an interface, a concrete class must specify that it
implements
(p. 421) the interface and
must declare each interface method with the signature specified in the interface declaration. A
class that does not implement all the interface's methods must be declared
abstract
.
• Implementing an interface is like signing a contract with the compiler that states, “I will declare
all the methods specified by the interface or I will declare my class
abstract
.”
• An interface is typically used when disparate (i.e., unrelated) classes need to share common meth-
ods and constants. This allows objects of unrelated classes to be processed polymorphically—ob-
jects of classes that implement the same interface can respond to the same method calls.
• You can create an interface that describes the desired functionality, then implement the interface
in any classes that require that functionality.