Java Reference
In-Depth Information
All the other methods of the class have not changed and can still be called just like
before. However, this time
Mammal
must be declared
abstract
because of the
walk
method
on line 20. Without the
abstract
keyword on line 1, the
Mammal
class would not compile.
The following two classes shown in Figure 2.13 are valid child classes of
Mammal
. The
Buffalo
class successfully overrides the
walk
method. The
Feline
class does not override
walk
, but it is declared
abstract
.
1. //Buffalo.java
2. public class Buffalo extends Mammal {
3. public void walk() {
4. System.out.println(“Buffalo is walking”);
5. }
6. }
7. //Feline.java
8. public abstract class Feline extends Mammal {
9. public void sleep() {
10. System.out.println(“Feline is sleeping”);
11. }
12. }
FIGURE 2.13
Buffalo
is a concrete subclass and
Feline
is an abstract subclass of
Mammal
.
Mammal is an
abstract class.
abstract Mammal
Feline is an
abstract subclass
of Mammal.
public abstract void walk()
Buffalo
abstract Feline
public void walk()
Buffalo is a concrete
subclass of Mammal.
abstract HouseCat
HouseCat needs to
be abstract if it does
not override walk().
Search WWH ::
Custom Search