1

Suppose I have an interface IBird. It has many methods such as eat(), walk(), run() and fly().

If a base class Ostrich wants to implement IBird, how should that go about it? Because Ostrich can't fly but can do all other stuff in IBird.

eliaspr
  • 302
  • 3
  • 15
nit710
  • 174
  • 1
  • 2
  • 12
  • 2
    Your class must implement the whole interface. No choice. The closest you could do is throw an exception if somebody is trying to make an `Ostrich` fly. But that would be considered bad practice. and might lead to unsuspected crash. – litelite Aug 11 '17 at 18:39
  • main communication for programmers is CODE, not story – Jacek Cz Aug 11 '17 at 18:43
  • Did you take it from a c# book/blog? Starting an interface with `I` is unusual for java. – Oleg Aug 11 '17 at 18:48
  • @JacekCz. Thx! That helped a lot, Zuckerberg! – nit710 Aug 11 '17 at 18:49
  • @Oleg, just followed C# conventions. I'm getting started with Java. – nit710 Aug 11 '17 at 18:52

4 Answers4

5

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() { ... }
}
eliaspr
  • 302
  • 3
  • 15
1

You can throw the UnsupportedOperationException in the implementation of fly method in Ostrich class.

class Ostrich {

    void fly() {
        throw new UnsupportedOperationException(); // throws the UnsupportedOperationException if someone tries to call this method
    }

    // implementation of eat(), walk() and run() 

}
Johny
  • 2,128
  • 3
  • 20
  • 33
0

If all birds cannot fly, fly() should probably not be declared in Bird.
You could introduce two interfaces to distinguish birds and flying birds.

A Bird interface that declares eat(), walk(), run() methods.
A FlyingBird interface that extends Bird and that declares a fly() method.

public interface Bird { 
    void eat();
    void walk();
    void run();
}

public interface FlyingBird extends Bird { 
   void fly();
}

public class Ostrich implements Bird {
    public void eat(){
      ...
    }
    public void walk(){
      ...
    }
    public void run(){
      ...
    }
}
davidxxx
  • 125,838
  • 23
  • 214
  • 215
-1

If you are using Java 8 you can also use the default keyword in an interface. This will allow you to write out a method to be used in the event that a class does not implement a method.

dblanken
  • 56
  • 2
  • 5
  • 2
    You can end with flying Ostriches if you do that. – Oleg Aug 11 '17 at 18:55
  • This is just a workaround so the OP doesn't have to put the method in the class. I never said it was a good workaround just an option. – dblanken Aug 11 '17 at 21:11