I am trying to understand inheritance from the Head First Java book. On page 193 I got everything right, and I am trying to invoke a method with a different parameter ( an overloaded method ), but the main class invokes the one inherited from the superclass. How can I invoke the following method?
boolean frighten(byte b) {
System.out.println("a bite?");
return true;
}
I tried to declare a byte, but it did not help. Thank you guys, here are the codes:
public class MonsterT {
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);
}
byte y = 2;
ma[0].frighten(y);
}
}
class Monster {
boolean frighten(int z) {
System.out.println("arrrgh");
return true;
}
}
class Vampire extends Monster {
boolean frighten(byte b) {
System.out.println("a bite?");
return true;
}
class Dragon extends Monster {
boolean frighten(int degree) {
System.out.println("breath fire");
return true;
}
And the output is: arrrgh
breath fire
arrrgh
arrrgh