Musical Instrument Class Hierarchy

Create a class MusicalInstrument and then define child classes StringInstrument, WindInstrument, PercussionInstrument, each with unique characteristics like number of strings for string instruments, type of mouthpiece for wind instruments, etc. Implement methods to print the details.

Example 1:
Input: instrument = StringInstrument("Guitar", 6)
Output: Type: Guitar, Strings: 6
Example 2:
Input: instrument = PercussionInstrument("Drums", "Snare Drum")
Output: Type: Drums, Percussion Type: Snare Drum

Use inheritance to create child classes and override the method to return the specific characteristics for each musical instrument type.

class MusicalInstrument:
    def details(self):
        pass

class StringInstrument(MusicalInstrument):
    def __init__(self, type, strings):
        self.type = type
        self.strings = strings

    def details(self):
        return f"Type: {self.type}, Strings: {self.strings}"

class WindInstrument(MusicalInstrument):
    def __init__(self, type, mouthpiece):
        self.type = type
        self.mouthpiece = mouthpiece

    def details(self):
        return f"Type: {self.type}, Mouthpiece: {self.mouthpiece}"

class PercussionInstrument(MusicalInstrument):
    def __init__(self, type, percussion_type):
        self.type = type
        self.percussion_type = percussion_type

    def details(self):
        return f"Type: {self.type}, Percussion Type: {self.percussion_type}"

instrument = StringInstrument("Guitar", 6)
print(instrument.details())

instrument = PercussionInstrument("Drums", "Snare Drum")
print(instrument.details())

© Let’s Data Science

LOGIN

Unlock AI & Data Science treasures. Log in!