Although they look the same, the old optical discs—CDs and DVDs—are quite different from each other. Let’s use them as the focus for creating a new interface.
Set up a local copy of the project:
In this activity, you will pair up to design a new custom interface.
Take a look at the prepared CD
and DVD
classes in the starter
code—only their signatures are present. You will add fields and methods by
extending a base class and implementing your interface. Before you do this,
however, you need to consider what code to place in the interface vs. the
abstract class vs. each specific class.
Just like you did with your Restaurant Menu studios, you will begin with pen and paper (or similar tools).
Here are a few behaviors that both CDs and DVDs share:
With your partner, add one or two more behaviors to the list. Feel free to do some quick research online if needed. Note that information like storage capacity does NOT belong on this list, since it describes a property (field) rather than a behavior (method).
For each behavior, identify if it depends on any type of instance variable.
For example, the spinning behavior does not require any field information
from the CD
or DVD
classes.
This collecting and sorting of behaviors is the first step toward designing your interface and abstract class. It prompts you to consider behaviors that can be generally applied vs. those that depend on data stored in a specific object.
Next, you and your partner need to decide which behaviors go into the interface vs. an abstract class.
Behaviors added to an interface do NOT have to be identical for every object
that implements the interface. The key is that the methods require no
object-specific data. The methods can accept parameters, but these should
represent generic values like String searchTerm
rather than field data
like DVD.title
.
For example, even though CDs and DVDs both spin, they do so at different
speeds. Their behavior (spinning) is the same, but the details of that
behavior vary. Thus, a method like spinDisc()
fits well into an interface.
CD
and DVD
classes.storageCapacity
). Consider:Now that your team has completed the before-coding thinking, it’s time to start building!
Add an OpticalDisc
file for the interface. Refer back to
Creating an Interface if you need a quick
review of the syntax.
Declare and initialize any constant fields.
Add a method signature for each of the behaviors you identified for the interface. For example:
void spinDisc();
Good work. Now you need to step back from the interface for a moment and take care of the remaining methods and fields.
To streamline the field and constructor code for CD
and DVD
, you
need to start with a base class.
BaseDisc
class and declare the fields used by both
CD
and DVD
.OK. Now put your code to work.
Extend the base class into the CD
and DVD
classes.
Implement the interface in each class.
Fill in the @Override
code for each of the interface methods.
Tip
You can hover over the implements
keyword and click the IntelliJ
suggestions to generate these method signatures.
Since these are signatures only, you will need to add specific code to each method.
Add getters, setters, and any other custom methods needed by each class.
Main
¶In the main
method, declare and initialize a CD
and DVD
object,
then run each of the behaviors you and your partner identified.
Example
1 2 | myCd.spinDisc();
myDvd.spinDisc();
|
Output
A CD spins at a rate of 200 - 500 rpm.
A DVD spins at a rate of 570 - 1600 rpm.
Wheel
and Frisbee
classes?