Summary of subtyping
IfS
and T
are types then
S
is a subtype of T
if:
S
andT
are interfaces andS
extendsT
.S
andT
are classes andS
extendsT
.S
is classT
is and interface, andS
implementsT
.
Warning! Things that are NOT True
Generics and Inheritance
- If Foo is a subtype of Goo, Foo<T> is a subtype of Goo<T>.
- If
S
is a subtype ofT
, thenFoo<S>
is NOT a subtype ofFoo<T>
.
Exceptions
The root of the class hierarchy for error handling is the class
Throwable
. Its name sounds like an interface but it is
actually a class.
Throwable
has two descendants, Error
and
Exception
. Errors generally describe conditions that
are fatal and graceful recovery from them in not possible. The result
is program death. Subclasses of this inclue StackOverflowError
and OutOfMemoryError
. Exceptions describe conditions
that are less drastic. You have seen these. These are all subclasses
of RuntimeException
NullPointerException
You called methods on an object that isnull
.ArrayIndexOutOfBoundsException
Yeah, you know this one.DivideByZeroException
You divided an integer by 0.NumberFormatException
You tried to parse an unparseable numerical string.
Checked Exceptions An exception is a checked
exception if it is not a subclass of RuntimeException
.
Code that can throw these must be placed in a try
block.
FileIO can raise checked exceptions, so you will learn how to handle them properly.