w3resource

Java: A Playable Interface with Football, Volleyball, and Basketball implementations

Java Interface: Exercise-8 with Solution

Write a Java program to create an interface Playable with a method play() that takes no arguments and returns void. Create three classes Football, Volleyball, and Basketball that implement the Playable interface and override the play() method to play the respective sports.

Sample Solution:

Java Code:

// Playable.java
// Declare the Playable interface
interface Playable {
    // Declare the abstract method "play" that classes implementing this interface must provide
    void play();
}

//Football.java
class Football implements Playable {
  public void play() {
    System.out.println("Playing football");
    // Add code to play football
  }
}
// Volleyball.java

// Declare the Volleyball class, which implements the Playable interface
class Volleyball implements Playable {
    // Implement the "play" method required by the Playable interface
    public void play() {
        // Print a message indicating that volleyball is being played
        System.out.println("Playing volleyball");
        // Additional code to play volleyball can be added here
    }
}
// Basketball.java

// Declare the Basketball class, which implements the Playable interface
class Basketball implements Playable {
    // Implement the "play" method required by the Playable interface
    public void play() {
        // Print a message indicating that basketball is being played
        System.out.println("Playing basketball");
        // Additional code to play basketball can be added here
    }
} 
// Main.java

// Declare the Main class
public class Main {
    public static void main(String[] args) {
        // Create instances of Playable objects for football, volleyball, and basketball
        Playable football = new Football();
        Playable volleyball = new Volleyball();
        Playable basketball = new Basketball();

        // Call the "play" method on each Playable object to play different sports
        football.play();
        volleyball.play();
        basketball.play();
    }
}

Sample Output:

Playing football
Playing volleyball
Playing basketball  

Explanation:

In the above exercise –

  • The "Football", "Volleyball", and "Basketball" classes implement the Playable interface and provide their own implementations of the play() method. Each class overrides the play() method to print a message indicating the sport being played. It may also include code to play the sport.
  • In the main() method, we create instances of the "Football", "Volleyball", and "Basketball" classes and assign them to variables of type Playable. We then call the play() method on each variable, which invokes the overridden implementation of the respective sport class.

Flowchart of Playable Java:

Flowchart: Playable Java

Flowchart of Football Java:

Flowchart: Football Java

Flowchart of Volleyball Java:

Flowchart: Volleyball Java

Flowchart of Basketball Java:

Flowchart: Basketball Java

Flowchart of Main Java:

Flowchart: Main Java

Java Code Editor:

Previous: Sortable interface with bubbleSort and selectionSort implementations.
Next: Searchable interface for document and Web page searching.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.