Java Reference
In-Depth Information
5.
6. public final void breathe() {
7. System.out.println(“Lion is breathing”);
8. }
9. }
No subclass of Lion can override the breathe method, but let's try it anyway and see
what happens. The following MountainLion class extends Lion and declares a breathe
method:
1. public class MountainLion extends Lion {
2. public void breathe() {
3. System.out.println(“MountainLion is breathing”);
4. }
5. }
As expected, this does not compile. Here is the compiler error that it generates:
MountainLion.java:2: breathe() in MountainLion cannot override breathe() in
Lion; overridden method is final
public void breathe() {
^
Declaring Abstract Classes
The exam objectives state that you should be able to “develop code that declares classes
(including abstract classes).” An abstract class is a class that cannot be instantiated. Use the
abstract keyword to declare a class as abstract, as demonstrated by the following Mammal
class:
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.
Search WWH ::




Custom Search