Reputation: 15
I can't understand why I'm getting an error on the line indicated below. if there is a specific topic which covers it please provide a line (or an explanation).
public interface Button {
void press();
}
public class Bomb implements Button {
public void press() {
System.out.println("Doesn't work");
}
void boom() {
System.out.println("Exploded");
}
}
public class Test {
public static void main(String[] args) {
Button redButton = new Bomb();
redButton.press();
redButton.boom(); // <-- Error is on this line.
}
}
Upvotes: 0
Views: 50
Reputation: 262474
Button redButton = new Bomb();
The interface Button
does not define a method boom()
.
The runtime instance may be a Bomb
(which has boom()
), but the compiler does not know that (it only sees the compile time type, which is Button
).
You need to use the interface defined by class Bomb
:
Bomb redButton = new Bomb();
Upvotes: 5