fork download
  1. // Abstract class Instrument
  2. abstract class Instrument {
  3. // Abstract method to be implemented by subclasses
  4. abstract void play();
  5.  
  6. // Abstract method to be implemented by subclasses
  7. abstract void tune();
  8. }
  9.  
  10. // Subclass Glockenspiel that extends Instrument
  11. class Glockenspiel extends Instrument {
  12. @Override
  13. void play() {
  14. System.out.println("Playing the Glockenspiel.");
  15. }
  16.  
  17. @Override
  18. void tune() {
  19. System.out.println("Tuning the Glockenspiel.");
  20. }
  21. }
  22.  
  23. // Subclass Violin that extends Instrument
  24. class Violin extends Instrument {
  25. @Override
  26. void play() {
  27. System.out.println("Playing the Violin.");
  28. }
  29.  
  30. @Override
  31. void tune() {
  32. System.out.println("Tuning the Violin.");
  33. }
  34. }
  35.  
  36. // Main class to test the program
  37. public class Main {
  38. public static void main(String[] args) {
  39. // Create instances of Glockenspiel and Violin
  40. Instrument glockenspiel = new Glockenspiel();
  41. Instrument violin = new Violin();
  42.  
  43. // Call methods on the instances
  44. glockenspiel.play();
  45. glockenspiel.tune();
  46.  
  47. violin.play();
  48. violin.tune();
  49. }
  50. }
  51.  
Success #stdin #stdout 0.07s 54640KB
stdin
Standard input is empty
stdout
Playing the Glockenspiel.
Tuning the Glockenspiel.
Playing the Violin.
Tuning the Violin.