My purpose is to create a superclass (let us call inpublic void play(); public void stop(); public void pause(); public void fastForward(); public void rewind(); public void changeVolTo(int level);
BasicMusicPlayer.java
) and let all other music players extend it.
But I don't know how (and impossible) to implement them in this class
What can I do?
public class BasicMusicPlayer
, we use the following code
Notice all the methods in an// save in BasicMusicPlayer.java public interface BasicMusicPlayer { void play(); void stop(); void pause(); void fastForward(); void rewind(); void changeVolTo(int level); }
interface
are public. So we can skip
the word public
in the body. You have to override all methods provided in the inteface.public class MP3Player implements BasicMusicPlayer { public void play(){ // your implementation } public void stop(){ // your implementation } public void pause(){ // your implementation } public void fastForward(){ // your implementation } public void rewind() { // your implementation } public void changeVolTo(int level){ // your implementation } // constructors and other methods here }
extends
)
one or more interface (the meaning is similar to class
).
Example we can have
public class MP3Player implements BasicMusicPlayer public class CDPlayer extends JFrame implements BasicMusicPlayer public class RecordablePlayer extends JFrame implements BasicMusicPlayer, RecordablehereRecordable
is another interface.The list of interfaces are seperated by comma.
Here are the differences :interface <--------------> class extends <--------------> implements
then thepublic double getPrice(BasicMusicPlayer player)
player
can be any reference type of class
MP3Player
, CDPlayer
... MP3Player
, CDPlayer
are
BasicMusicPlayer
.
public
. And you end every method declaration with ";".
public static final
member for class
member. Here is an example:
public interface BasicMusicPlayer { final int MAX_LEVEL = 10; void play(); void stop(); void pause(); void fastForward(); void rewind(); void changeVolTo(int level); }
javax.swing.SwingConstants
. The class provide
most of the constants Swing classes use. eg.
NORTH, SOUTH, TOP, BOTTOM, LEFT, RIGHT ...
public class ClassName implements interfaces(seperated by ",") { // your code } or public class ClassName extends SuperClass implements interfaces(seperated by ",") { // your code }
final static
).