w3resource

Java: Abstract Instrument Class with Glockenspiel and Violin Subclasses

Java Abstract Class: Exercise-9 with Solution

Write a Java program to create an abstract class Instrument with abstract methods play() and tune(). Create subclasses for Glockenspiel and Violin that extend the Instrument class and implement the respective methods to play and tune each instrument.

Sample Solution:

Java Code:

//Instrument.java
// Define an abstract class named Instrument
abstract class Instrument {
  // Declare an abstract method play
  public abstract void play();

  // Declare an abstract method tune
  public abstract void tune();
}

//Glockenspiel.java
// Define a class named Glockenspiel that extends the Instrument class
class Glockenspiel extends Instrument {
  // Override the play method from the Instrument class
  @Override
  public void play() {
    // Print a message about playing the glockenspiel
    System.out.println("Glockenspiel: Playing the notes on the metal bars.");
  }

  // Override the tune method from the Instrument class
  @Override
  public void tune() {
    // Print a message about tuning the glockenspiel
    System.out.println("Glockenspiel: Tuning the metal bars to the correct pitch.");
  }
} 
//Violin.java
// Define a class named Violin that extends the Instrument class
class Violin extends Instrument {
  // Override the play method from the Instrument class
  @Override
  public void play() {
    // Print a message about playing the violin
    System.out.println("Violin: Playing the strings with a bow or fingers.");
  }

  // Override the tune method from the Instrument class
  @Override
  public void tune() {
    // Print a message about tuning the violin
    System.out.println("Violin: Tuning the strings to the correct pitch.");
  }
}
//Main.java
// Define the Main class
public class Main {
  // The main method, the entry point of the program
  public static void main(String[] args) {
    // Create an instance of Glockenspiel as an Instrument
    Instrument glockenspiel = new Glockenspiel();
    // Create an instance of Violin as an Instrument
    Instrument violin = new Violin();

    // Call the play method on the glockenspiel object
    glockenspiel.play();
    // Call the tune method on the glockenspiel object
    glockenspiel.tune();

    // Call the play method on the violin object
    violin.play();
    // Call the tune method on the violin object
    violin.tune();
  }
} 

Output:

Glockenspiel: Playing the notes on the metal bars.
Glockenspiel: Tuning the metal bars to the correct pitch.
Violin: Playing the strings with a bow or fingers.
Violin: Tuning the strings to the correct pitch.

Explanation:

In the above exercise -

  • The abstract class "Instrument" has two abstract methods: play() and tune(). The subclasses Glockenspiel and Violin extend the Instrument class and provide their own implementations for these abstract methods.
  • The "Glockenspiel" class describes how a glockenspiel plays the notes on the metal bars and tunes them to the correct pitch, while the "Violin" class describes how a violin plays the strings with a bow or fingers and tunes the strings to the correct pitch.
  • In the main method, we create instances of Glockenspiel and Violin, and then call the play() and tune() methods on each object to demonstrate how each instrument plays and tunes.

Flowchart:

Flowchart: Instrument Java
Flowchart: Glockenspiel Java
Flowchart: Violin Java
Flowchart: Main Java

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Abstract Person Class with Athlete and LazyPerson Subclasses.
Next: Abstract Shape2D Class with Rectangle and Circle Subclasses.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.