Java Reference
In-Depth Information
You also can define a method to be final; a final method cannot be overridden by its sub-
classes.
For example, the following method m is final and cannot be overridden:
public class Test {
// Data fields, constructors, and methods omitted
public void m() {
// Do something
final
}
}
Note
The modifiers public , protected , private , static , abstract , and final are
used on classes and class members (data and methods), except that the final modi-
fier can also be used on local variables in a method. A final local variable is a constant
inside a method.
11.37
How do you prevent a class from being extended? How do you prevent a method
from being overridden?
Check
Point
11.38
Indicate true or false for the following statements:
a. A protected datum or method can be accessed by any class in the same package.
b. A protected datum or method can be accessed by any class in different packages.
c. A protected datum or method can be accessed by its subclasses in any package.
d. A final class can have instances.
e. A final class can be extended.
f.
A final method can be overridden.
K EY T ERMS
actual type 422
casting objects 425
constructor chaining
override 419
polymorphism 422
protected 437
single inheritance
415
declared type 422
dynamic binding
414
422
subclass 408
subtype 421
superclass 408
supertype 421
type inference
inheritance 408
instanceof 426
is-a relationship
437
method overriding
419
431
multiple inheritance
414
C HAPTER S UMMARY
1.
You can define a new class from an existing class. This is known as class inheritance .
The new class is called a subclass , child class , or extended class . The existing class is
called a superclass , parent class , or base class .
 
 
Search WWH ::




Custom Search