Java Reference
In-Depth Information
Notice there are no curly braces—not even empty braces. An abstract method does not contain
a method body. Declaring a method as abstract in a class has the following consequences:
The enclosing class must be declared abstract .
Any concrete subclass must override all the abstract methods inherited from the parent
class.
If a subclass does not override its parent's abstract methods, the subclass must also be
declared abstract .
Concrete Subclasses
Because an abstract method does not have any implementation, its class must be
abstract. Otherwise, instances of the class could attempt to invoke the abstract method,
which doesn't make sense because the abstract method does not contain any code. The
term concrete subclass refers to a subclass that is not abstract. Child classes that do
not want to be abstract must override the abstract methods in the parent or be abstract
classes themselves.
Let's look at an example. The following Mammal class is similar to the previous version,
with the addition of an abstract method named walk :
1. public abstract class Mammal {
2. public boolean hasFur;
3.
4. public Mammal() {
5. hasFur = false;
6. }
7.
8. public Mammal(boolean hasFur) {
9. this.hasFur = hasFur;
10. }
11.
12. public void breathe() {
13. System.out.println(“Mammal is breathing”);
14. }
15.
16. public void eat(String something) {
17. System.out.println(“Mammal is eating “ + something);
18. }
19.
20. public abstract void walk();
21. }
Search WWH ::




Custom Search