Incompatible types with new class?

hosam hassn :

In this example, where I have interface and I have class , when I write this code

MediaPlayer P = new MP3;

The problem happens. Knowing that I applied the same line to another example and there is no problem. I need to fix the problem and understand why it's necessary

image

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MediaPackage player = new MP4();
        MediaPlayer P = new MP3();

    }

}


/// 
public interface MediaPackage {
    void playFile(String filename);
}

/// 

public interface MediaPlayer {
    void play(String filename);
}

// 

public class MP3 implements MediaPlayer {


  @Override
  public void play(String filename){
    System.out.println("Playing MP3 File " + filename);
  }



}

// 

public class MP4 implements MediaPackage{

@Override
public void playFile(String filename){
    System.out.println("Playing MP4 File " + filename);
   }
}
lealceldeiro :

In the following piece of code, you're using (without knowing it) as reference Android Media Player (android.media.MediaPlayer) instead of your MediaPlayer interface (com.ahmedco.adapter.example2.MediaPlayer).

MediaPlayer P = new MP3();

So, maybe change it to:

com.ahmedco.adapter.example2.MediaPlayer P = new MP3();

Or simply change the name of the interface to something else, for example, MyMediaPlayer, and use it like:

MyMediaPlayer P = new MP3();

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=135735&siteId=1