'Java Head First Chapter 7 MonsterTestDrive Exercise

enter image description here enter image description here

I've got a problem with exercise in Java Head First. Basically I don't understand why output changes based on return true or false. Why does return value have anything to do with print?

public class MonsterTestDrive {
    public static void main(String [] args) {
        Monster [] ma = new Monster[3];
        ma[0] = new Vampire();
        ma[1] = new Dragon();
        ma[2] = new Monster();
        for(int x = 0; x < 3; x++) {
            ma[x].frighten(x);
        }
    }
}
class Monster {

// A

} 
class Vampire extends Monster {

// B

}
class Dragon extends Monster {
    boolean frighten(int degree) {
        System.out.println(“breath fire”);
        return true;
    }
}

Which of the A-B pairs of methods listed on the right, if inserted into the classes on the left, would compile and produce the output shown?

(The A method inserted into class Monster, the B method inserted into class Vampire.)

//1

boolean frighten(int d) {     
    System.out.println(“arrrgh”); //A
    return true;
}
boolean frighten(int x) {
    System.out.println(“a bite?”);//B
    return false;
}

//2

boolean frighten(int x) {
    System.out.println(“arrrgh”);//A
    return true;
}
int frighten(int f) {
    System.out.println(“a bite?”);//B
    return 1;
}

//3

boolean frighten(int x) {
    System.out.println(“arrrgh”); //A
    return false;
}
boolean scare(int x) {
    System.out.println(“a bite?”);//B
    return true;
}

//4

boolean frighten(int z) {
    System.out.println(“arrrgh”);//A
    return true;
}
boolean frighten(byte b) {
    System.out.println(“a bite?”);//B
    return true;
}

The correct answer is option /1.



Solution 1:[1]

I don't understand why output changes based on return true or false

Because that's not the important difference between the examples. The point here is that Vampire (i.e. B) inherits from Monster (i.e. A) and we want the behavior of calling frighten to change from the default of printing "arrrgh" and print "a bite?" instead. To do that we need to override frighten in the Vampire class.

The catch is that the base method and the overriding one must have

  1. the same name
  2. the same return type and
  3. the same arguments (meaning same number and same type, not necessarily same name).

All three of these conditions must be true.


  1. This satisfies all three conditions

    boolean frighten(int d) {     
        System.out.println(“arrrgh”); //A
        return true;
    }
    boolean frighten(int x) {
        System.out.println(“a bite?”);//B
        return false;
    }
    

  1. Here B has a different return type, so it doesn't satisfy condition 2

    boolean frighten(int x) {
        System.out.println(“arrrgh”);//A
        return true;
    }
    int frighten(int f) {
        System.out.println(“a bite?”);//B
        return 1;
    }
    

  1. Here B has another name entirely, so it doesn't satisfy condition 1.

    boolean frighten(int x) {
        System.out.println(“arrrgh”); //A
        return false;
    }
    boolean scare(int x) {
        System.out.println(“a bite?”);//B
        return true;
    }
    

  1. Here the argument type for B is byte instead of int, so it doesn't satisfy condition 3.

    boolean frighten(int z) {
        System.out.println(“arrrgh”);//A
        return true;
    }
    boolean frighten(byte b) {
        System.out.println(“a bite?”);//B
        return true;
    }
    

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Federico klez Culloca