Java Reference
In-Depth Information
d. An abstract class can extend another abstract class.
e. An abstract class can extend a non-abstract class.
f. An interface is an abstract class.
g. An interface can declare instance data.
h. Any method in an interface must be public.
i. All methods in an interface must be abstract.
j. An interface can have no methods at all.
k. An interface can extend another interface.
l. An interface can declare constructors.
m. A class may extend more than one class.
n. A class may implement more than one interface.
o. A class may extend one class and implement one interface.
p. An interface may implement some of its methods.
q. Methods in an interface may provide a
throws
list.
r. All methods in an interface must have a
void
return type.
s.
Throwable
is an interface.
t.
Object
is an abstract class.
u.
Comparable
is an interface.
v.
Comparator
is an example of an interface that is used for function
objects.
Carefully examine the online documentation for the
Scanner
construc-
tors. Which of the following are acceptable parameters for a
Scanner:
File
,
FileInputStream
,
FileReader
.
4.18
A local class can access local variables that are declared in that
method (prior to the class). Show that if this is allowed, it is possible
for an instance of the local class to access the value of the local vari-
able, even after the method has terminated. (For this reason, the com-
piler will insist that these variables are marked
final
.)
4.19
This exercise explores how Java performs dynamic dispatch, and also
why trivial final methods may not be inlined at compile time. Place
each of the classes in Figure 4.49 in its own file.
a.
4.20
Compile
Class2
and run the program. What is the output?
b.
What is the exact signature (including return type) of the
getX
method that is deduced at compile time at line 14?
c.
Change the
getX
routine at line 5 to return an
int
; remove the
""
from the body at line 6, and recompile
Class2
. What is the output?
d.
What is the exact signature (including return type) of the
getX
method that is now deduced at compile time at line 14?
e.
Change
Class1
back to its original, but recompile
Class1
only.
What is the result of running the program?
f.
What would the result have been had the compiler been allowed
to perform inline optimization?
Search WWH ::
Custom Search