Object Oriented Drum Kit: An OOP Class Example

This class creates a simple drum rhythm. Its purpose is mainly to show how a program can be written with a constructor that allows it to be instantiated. Such instantiating, multiple version making, is one of the basic features of object oriented programming (OOP). A version (instantiation) of a class with a constructor can be called by another class, so often such classes do not have a main() method. This one does, however, and the next demo will demonstrate the use of several classes.

Object oriented programming involves many other features than instantiation. To get more of an idea get a good Java book and read up on it.

This is what the class sounds like when run: (Exactly the same as the Static Kit.)

Click here to view source ..

Let's have a closer look.

import jm.JMC;
import jm.music.data.*;
import jm.midi.*;
import jm.music.tools.*;
import jm.util.*;

* An example which generates a drum kit pattern
* and writes to a MIDI file called OOP_kit.mid
* This version uses instantiated methods in the one class.
* @author Andrew Brown

public final class OOP_Kit implements JMC{
public static void main(String[] args){
new OOP_Kit();
The class starts in the normal way by importing, class declaration, and creating the main() method. This is the entire main method!! All it does it create a new version of (instantiate) the OOP_Kit() class - this class:

     public OOP_Kit() {
Score pattern1 = new Score(
"Object oriented class example");
// 9 = MIDI channel 10
Part drums = new Part("Drums",9);
Phrase phrBD = new Phrase();
Phrase phrSD = new Phrase();
Phrase phrHH = new Phrase();
CPhrase phrEnd = new CPhrase();

//calling methods below
phrBD = KickPattern();
phrSD = SnarePattern();
phrHH = HatsPattern();
phrEnd = EndPattern();

// loop the drum pattern
int loopNum = 7;
Mod.repeat(phrBD, loopNum);
Mod.repeat(phrSD, loopNum);
Mod.repeat(phrHH, loopNum);

// add phrases to the instrument (part)

// add the drum part to a score.

// write the score to a MIDIfile
Write.midi(pattern1, "OOP_Kit.mid");
This OOP_Kit() method does all the work done by the main() method in the previous example. The program is the same but the Java structure to achieve it is different. This method is called the constructor method. It has several unique features. firstly, its name is the same as a the class name. Secondly, it has no return type, not even void (which means no returned value). These are unique features of constructors.

private Phrase KickPattern() {
// make bass drum
Phrase phrase = new Phrase(0.0);
for(int i=0;i<4;i++){
Note note = new Note(36, C);
Note rest = new Note(REST, C);
return phrase;
The next method in this class is the KickPattern() method. This is identical to the previous example, but the method is NOT static because we want it instantiate it with the rest of the class. The only other feature to look out for is that when the Phrase is declared in this method it is given a start time of 0.0, the phrase declared above had none because we knew it would be overwritten with this phrase.

Each of the snarePattern and hihatPattern methods look very similar to this one, so we won't show them here.

Remember that the EndPattern() method is different in two ways. Firstly, it uses a CPhrase rather than a Phrase so that we can conveniently have have two drums sounding together. Secondly, the CPhrase is declared with no startTime argument. This means that when it is added to the Part it will be placed at the end of the other phrases. This is also convenient because it allows us to make the earlier patterns of any length or have any number of loops and the CPhrase will always be placed at the end.

jMusic Tutorial Index