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
- Class can implement multiple interfaces
- Must implement all methods from both
- Use comma to separate interfaces
Related Inheritance exercises
- Practice Vehicle Speed Calculator in Java
- Practice Bank Account Types in Java
- Practice Comparable Implementation in Java
Practice all Inheritance exercises · Run this idea in the Java compiler