Java Reference
In-Depth Information
You also can define a method to be final; a final method cannot be overridden by its
subclasses.
For example, the following method m is final and cannot be overridden:
public class Test {
// Data fields, constructors, and methods omitted
public final void m() {
// Do something
}
}
Note
The modifiers public , protected , private , static , abstract , and final are
used on classes and class members (data and methods), except that the final modifier
can also be used on local variables in a method. A final local variable is a constant
inside a method.
11.41
How do you prevent a class from being extended? How do you prevent a method from
being overridden?
Check
Point
11.42
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 424
casting objects 427
constructor chaining
override 000
polymorphism 423
protected 440
single inheritance
417
declared type 424
dynamic binding
416
424
subclass 410
subtype 423
superclass 410
supertype 423
type inference
inheritance 410
instanceof 428
is-a relationship
440
method overriding
419
433
multiple inheritance
416
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 .
2. A constructor is used to construct an instance of a class. Unlike properties and methods,
the constructors of a superclass are not inherited in the subclass. They can be invoked
only from the constructors of the subclasses, using the keyword super .
 
 
Search WWH ::




Custom Search