In: Computer Science
in java Write an interface for a CD player. It should have the standard operations (i.e. play, stop, nextTrack, previousTrack, etc.) that usual CD players have.
Interface in java
interface is defined as includes group of methods without body. it is a fully abstract class .In interface all the fields are public, static and final by default.Use interface keyword to create an interface and implements keyword is used for implement interface in class.
//create interface CDPlayer
interface CDPlayer{
//methods are public abstract by default and they have no
body
void play(); //method for play
void stop(); //method for stop
void nextTrack(); //method for nextTrack
void previousTrack(); //method for previousTrack
}
//create class CDPlayers and implements interface
class CDPlayers implements CDPlayer{
//implement play abstract method
public void play()
{
System.out.println("Play Songs"); //print the message when methos
is call
}
//implement stop abstract method
public void stop()
{
System.out.println("Stop songs"); //print the message when methos
is call
}
//implement nextTrack abstract method
public void nextTrack()
{
System.out.println("Next song select for play"); //print the
message when methos is call
}
//implement previousTrack abstract method
public void previousTrack()
{
System.out.println("Previous song select for play"); //print the
message when methos is call
}
}
public class Main
{
public static void main(String[] args) {
CDPlayers cd = new CDPlayers();
//create object of CDPlayers class
cd.play(); //call play method
cd.stop(); //call stop method
cd.nextTrack(); //call nextTrack
method
cd.previousTrack(); //call
previousTrack method
}
}
=======================================OUTPUT===============================
==Please Upvote==