Week 2: Classes and Interfaces
Today we’ll explore how object-oriented programming (OOP) in Java uses
class hierarchies, inheritance (via extends
), and interface
implementation (via implements
).
We’ll be modeling musical instruments using Java’s OOP facilities. To make things fun, we’ll leave the details up to you, but here’s the general idea.
-
Your model should account for at least 4 concrete musical instruments. You pick which, but make sure there’s some variety!
-
There are many different kinds of musical instruments. Model at least two different kinds, like:
-
Wind instruments, which are further subdivided into woodwinds (clarinet, oboe, bassoon) and brass (trumpet, tube, trombone, saxophone). There are others, too, like the harmonica and melodica.
-
String instruments, like guitars and sarods and ouds and zhengs.
-
Percussion instruments, like the tabor or the triangle. Many percussion instruments are unpitched, but some can play a wide variety of notes, like the steel drum or xylophone.
-
-
Your model should model at least two qualities of each instrument, like:
-
What’s it made of?
-
What’s its range? (You can use
int
s to talk about frequencies, or just wing it withString
s.) -
What’s its natural key?
-
Can you march with it?
-
What noise does it make?
-
Can you play it in more than one way? For example, a standup bass is typically plucked, but it can also be played col arco, i.e., with a bow.
-
-
One of your qualities should only apply to some instruments. For example, only percussion instruments have “striker” (e.g., a drum stick or a hand or a mallet), and only stringed instruments have “courses” of strings (e.g., a guitar has six single-string courses; a mandolin has four double-string courses).
Your CAs will guide you through the process of thinking about these things, but the general process will be:
- Think about how to structure a class hierarchy (with inheritance,
using
extends
) for describing the instruments you choose. First, just draw it on paper… then, after talking with other students in small groups, try to write it in Java. - Think about how to _re_structure your class hierarchy to use
less inheritance with
extends
and more interface implementation withimplements
. What changes about your hierarchy? What interfaces are useful?