Inteface

You should refer to Creating Interfaces or the text book 228-234.

Question

  1. If I have a set of methods that should be provided by certain classes, but I don't know how to implement these methods now. What should I do?.
    For example, I want to create a computer program that works like a music player (like MP3 player, or CD player). I know that they all should have the following methods.
    public void play();
    public void stop();
    public void pause();
    public void fastForward();
    public void rewind();
    public void changeVolTo(int level);
    
    My purpose is to create a superclass (let us call in 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?
  2. How can I create a class that extends two or more classes?

Answer

  1. You know all the method names. But you don't know how to implement them. You can create a class that provides the method declarations only, without providing the methods' body.
    This is very similar to header files in C++.
    Instead of use public class BasicMusicPlayer, we use the following code
    // save in BasicMusicPlayer.java
    public interface BasicMusicPlayer { 
        void play();               
        void stop();                  
        void pause();                
        void fastForward();            
        void rewind();                
        void changeVolTo(int level); 
    }
    
    Notice all the methods in an interface are public. So we can skip the word public in the body.
    The way to use it is
    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
    }
    
    You have to override all methods provided in the inteface.
  2. It is impossible to extends 2 or more classes at the same time. You can do it in C++ but you can't do it in java.
    However, we can implements (the meaning is similar to 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, Recordable
    
    here Recordable is another interface.The list of interfaces are seperated by comma.
  3. Here are the correspondings :
    interface  <--------------> class
    extends    <--------------> implements
    
    Here are the differences :

Using an Interface as a Type

When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name.
For example, I can have a method name
public double getPrice(BasicMusicPlayer player) 
then the player can be any reference type of class MP3Player , CDPlayer ...
that means if a class implements an interface. The class is the interface. Both MP3Player , CDPlayer are BasicMusicPlayer .

Summary


Definition: An interface is a named collection of method definitions (without implementations). An interface can also declare constants.
  • Implements an interface by
    public class ClassName implements interfaces(seperated by ",") {
      // your code
    }
    or
    public class ClassName extends SuperClass implements interfaces(seperated by ",") {
      // your code
    }
    
  • If you implements an interface, you have to override all the methods declared in the interface.
  • "implements" has a similar meaning of "extends". If a class implements an interface, the class can access all the public variables (all of them are final static).
  • You can use interface names anywhere you can use any other data type name. A class implements an interface is the interface.