You could make Ostrich
abstract
. That might work in some situations, but not here since every instance of Ostrich
would have to implement the missing functionality.
Another choice would be, as Johny
pointed out, to throw a UnsupportedOperationException
. But that might result in unexpected crashes which aren't good for the user.
A third way is to remove the method fly()
from the interface IBird
and only leave the stuff that all birds share. Then you make another interface IBirdThatCanFly
which extends IBird
. Then you can add the missing fly()
method.
public interface IBird { //all birds
public void eat();
public void walk();
public void run();
}
public interface IBirdThatCanFly extends IBird { //birds that can fly
public void fly();
}
public class Ostrich implements IBird { //Ostrich can't fly
public void eat() { ... }
public void walk() { ... }
public void run() { ... }
}