Multiple Interfaces in Java: Explanation & Practice

Implement multiple interfaces

Problem summary

Create Flyable and Swimmable interfaces. Duck implements both.

Starter code

// Flyable: void fly()
// Swimmable: void swim()
// Duck implements both

public class Main {
    public static void main(String[] args) {
        // Create duck and call both abilities
    }
}

Expected output and test cases

  • Duck abilities
    Flying at 20 km/h
    Swimming at 5 km/h
  • Eagle flying only
    Flying at 50 km/h
  • Fish swimming only
    Swimming at 30 km/h

Hints

  1. Class can implement multiple interfaces
  2. Must implement all methods from both
  3. Use comma to separate interfaces

Related Inheritance exercises

Practice all Inheritance exercises · Run this idea in the Java compiler