Search code examples
javainterfaceoverridingabstract-classimplements

"Class is not abstract and does not override method in class" error, but I implemented those methods


I know there are lots of questions related to this compiler error in Java. I have to code the behavior of a simple media player that plays images, audio files and videos. I wrote seven classes, three of them being interfaces. Here's a schematic:

  • AudioI contains volume-related methods: weaker, louder, getVolume and setVolume;
  • VideoI contains brightness-related methods: brighter, darker, getBrightness and setBrightness;
  • MediaI contains general methods for media: play, getTitle, setTitle, getDuration and setDuration.

The other classes are:

  • Audio: it implements AudioI, MediaI and extends Media;
  • Image: it implements VideoI, MediaI and extends Media;
  • Video: it implements VideoI, MediaI and extends Media;
  • Media: it implements AudioI, VideoI and MediaI;
  • InterfaceTest: where I test methods and constructors.

Now, the problem I have is that, even though I think I implemented everything correctly, I get the compiler error in the title. Also, every time I compile, the compiler outputs 4 errors only, related to Audio, Image, Video and Media classes. If I remove one signature in a random interface the compiler switches to another method and tells me it's wrongly implemented. Even if I remove all method signatures except for one, the compiler outputs the error regarding that particular method.

Also, I don't know if this is helpful, I use Java 8 and I don't use an IDE, I compile and run the files from the Windows terminal.

Here's the code:

AudioI interface

public interface AudioI{
  public void louder();
  public void weaker();
  public int getVolume();
  public void setVolume();
}

VideoI interface

public interface VideoI{
  public void brighter();
  public void darker();
  public int getBrightness();
  public void setBrightness();
}

MediaI interface

public interface MediaI{
  public void play();
  public int getDuration();
  public void setDuration();
  public String getTitle();
  public void setTitle();
}

Audio class

public class Audio extends Media implements AudioI, MediaI{
  private int volume;
  private int duration;
  private String title;

  /**
    CONSTRUCTORS
  **/
  Audio(){
    this.title = "audio.mp3";
    this.duration = 100;
    this.volume = 5;
  };

  Audio(String title, int duration){
    this.title = title;
    this.duration = duration;
    this.volume = 5;
  };

  /**
    AUDIOI METHODS IMPLEMENTATION
  **/
  public void louder(){
    if(this.volume < 10){
        this.volume++;
    }else{
        System.out.print("Maximum volume reached");
    }
  };

  public void weaker(){
    if(this.volume > 0){
        this.volume--;
    }else{
        System.out.print("Minimum volume reached");
    }
  };

  public int getVolume(){
    return this.volume;
  };

  public void setVolume(int volume){
    if(volume > 9){
        System.out.println("******************************");
        System.out.println("* ERROR: maximum volume is 9 *");
        System.out.println("******************************");
    }else if(volume < 0){
        System.out.println("******************************");
        System.out.println("* ERROR: minimum volume is 0 *");
        System.out.println("******************************");
    }else{
        this.volume = volume;
    }
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/
  public void play(){
    int i;

    System.out.println("TITLE: " + this.title);

    System.out.print("VOLUME: >");
    for(i = 0; i < this.volume; i++){
        System.out.print(")");
    }
    System.out.println("");
  };

  public int getDuration(){
    return this.duration;
  };

  public void setDuration(int duration){
    this.duration = duration;
  };

  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };
}

Image class

public class Image extends Media implements VideoI, MediaI{
  private String title;
  private int brightness;
  private int duration = 1;

  /**
    CONSTRUCTORS
  **/
  Image(){
    this.title = "img.jpeg";
    this.brightness = 5;
  };

  Image(String title){
    this.title = title;
    this.brightness = 5;
    this.duration = 1;
  };

  /**
    AUDIOI METHODS IMPLEMENTATION
  **/
  public void brighter(){
    if(this.brightness < 10){
        this.brightness++;
    }else{
        System.out.print("Maximum brightness reached.");
    }
  };

  public void darker(){
    if(this.brightness > 0){
        this.brightness--;
    }else{
        System.out.print("Minimum brightness reached.");
    }
  };

  public int getBrightness(){
    return this.brightness;
  };

  public void setBrightness(int brightness){
    if(brightness > 10){
        System.out.println("* ERROR: maximum brightness is 10 *");
    }else if(brightness < 1){
        System.out.println("* ERROR: minimum brightness is 1 *");
    }else{
        this.brightness = brightness;
    }
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/
  public void play(){
    int i;

    System.out.println("TITOLO: " + this.title);

    System.out.print("LUMINOSITA': >");
    for(i = 0; i < this.brightness; i++){
        System.out.print("*");
    }
    System.out.println("");
  };

  public int getDuration(){
    return this.duration;
  };

  public void setDuration(int duration){
    System.out.println("ERROR: cannot set image duration");
  };

  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };
}

Video class

import java.util.ArrayList;

public class Video extends Media implements VideoI, MediaI{
  private int duration;
  private String title;
  private int brightness;
  private Image buff;
  ArrayList<Image> images = new ArrayList<Image>();

  /**
    CONSTRUCTORS
  **/
  Video(){
    for(int i = 0; i < 100; i++){
        images.add(new Image("img" + i + ".jpeg"));
    }
    this.brightness = 5;
    this.duration = 100;
    this.title = "slideshow.mkv";
  };

  Video(String title, int nOfImages){
    for(int i = 0; i < nOfImages; i++){
        buff = new Image("img" + i + ".jpeg");
        this.images.add(buff);
    }
    this.brightness = this.images.get(0).getBrightness();
    this.duration = nOfImages;
    this.title = title;
  };

  /**
    VIDEOI METHODS IMPLEMENTATION
  **/
  public void brighter(){
    int i;

    if(this.brightness < 10){
        this.brightness++;

        for(i = 0; i < this.images.size(); i++){
            this.images.get(i).setBrightness(this.brightness);
        }
    }else{
        System.out.print("Maximum brightness reached.");
    }
  };

  public void darker(){
    int i;

    if(this.brightness > 0){
        this.brightness--;

        for(i = 0; i < this.images.size(); i++){
            this.images.get(i).setBrightness(this.brightness);
        }
    }else{
        System.out.print("Minimum brightness reached.");
    }
  };

  public int getBrightness(){
    return this.brightness;
  };

  public void setBrightness(int brightness){
    if(brightness < 0){
        System.out.println("**********************************");
        System.out.println("* ERROR: minimum brightness is 0 *");
        System.out.println("**********************************");
    }else if(brightness > 10){
        System.out.println("***********************************");
        System.out.println("* ERROR: maximum brightness is 10 *");
        System.out.println("***********************************");
    }else{
        for(int i = 0; i < this.images.size(); i++){
            this.images.get(i).setBrightness(brightness);
            this.brightness = brightness;
        }
    }
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/
  public void play(){
    int i;

    System.out.println("TITOLO: " + this.title);

    System.out.print("LUMINOSITA': >");
    for(i = 0; i < this.brightness; i++){
        System.out.print("*");
    }
    System.out.println("");


    System.out.print("DURATA VIDEO: ");
    for(i = 0; i < this.images.size(); i = i + 10){
        System.out.print("[] ");
    }
    System.out.println("");
  };

  public int getDuration(){
    this.duration = this.images.size();
    return this.duration;
  };

  public void setDuration(int duration){
    System.out.println("ERROR: cannot set video duration");
  };

  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };
}

Media class

import java.util.ArrayList;

public class Media implements AudioI, VideoI, MediaI{
  private Video video;
  private Audio audio;
  private String title;
  private int duration;
  private int volume;
  private int brightness;
  private boolean hasVideo;
  private boolean hasAudio;

  /**
    CONSTRUCTORS
  **/

  Media(){
    this.title = "Video.mkv";
    this.volume = 5;
    this.brightness = 5;
    this.hasVideo = false;
    this.hasAudio = false;
  };

  Media(String title, Audio audio, Video video){
    this.video = video;
    this.audio = audio;
    this.title = title + ".mkv";
    this.volume = audio.getVolume();
    this.brightness = video.getBrightness();
    if(this.audio.getDuration() >= this.video.getDuration()){
        this.duration = this.audio.getDuration();
    }else{
        this.duration = this.video.getDuration();
    }
    this.hasVideo = true;
    this.hasAudio = true;
  };

  Media(String title, Audio audio){
    this.audio = audio;
    this.title = title + ".mp3";
    this.volume = audio.getVolume();
    this.duration = this.audio.getDuration();
    this.hasVideo = false;
    this.hasAudio = true;
  };

  Media(String title, Video video){
    this.video = video;
    this.title = title + ".sdw";
    this.brightness = video.getBrightness();
    this.duration = this.video.getDuration();
    this.hasVideo = true;
    this.hasAudio = false;
  };

  /**
    MEDIAI METHODS IMPLEMENTATION
  **/

  public void play(){
    int i;

    System.out.println("TITOLO: " + this.title);

    if(this.hasAudio == true){
        System.out.print("VOLUME: >");
        for(i = 0; i < this.audio.getVolume(); i++){
            System.out.print(")");
        }
        System.out.println("");
    }

    if(this.hasVideo == true){
        System.out.print("LUMINOSITA': >");
        for(i = 0; i < this.video.getBrightness(); i++){
            System.out.print("*");
        }
        System.out.println("");
    }

    if(this.hasVideo == true && this.hasAudio == true){
        System.out.print("DURATA AUDIO: ");
        for(i = 0; i < this.video.getDuration(); i = i + 10){
            System.out.print("[] ");
        }
        System.out.println("");
        System.out.print("DURATA VIDEO: ");
        for(i = 0; i < this.audio.getDuration(); i = i + 10){
            System.out.print("[] ");
        }
        System.out.println("");
    }
  };

  public void setAudio(Audio audio){
    this.audio = audio;
  };


  public void setVideo(Video video){
    this.video = video;
  };

  public int getDuration(){
    return this.duration;
  };

  public void setDuration(int duration){
    System.out.println("ERROR: cannot modify media duration");
  };


  public String getTitle(){
    return this.title;
  };

  public void setTitle(String title){
    this.title = title;
  };

  /**
    AUDIOI METHODS IMPLEMENTATION
  **/

  public void louder(){
    this.audio.louder();
  };

  public void weaker(){
    this.audio.weaker();
  };

  public int getVolume(){
    return this.audio.getVolume();
  };

  public void setVolume(int volume){
    this.audio.setVolume(volume);
  };

  /**
    VIDEOI METHODS IMPLEMENTATION
  **/

  public void brighter(){
    this.video.brighter();
  };

  public void darker(){
    this.video.darker();
  };

  public int getBrightness(){
    return this.video.getBrightness();
  };

  public void setBrightness(int brightness){
    this.video.setBrightness(brightness);
  };
}

Solution

  • the problem seems to be with your set methods, within the interfaces, you've declared that the set methods will not have a parameter but within the classes that implement those interfaces you instead provided parameters for them i.e you're not implementing the appropriate methods from the interfaces but rather defining a new method completely different to the ones within the interfaces.

    The solution is simply to change the set method definitions of the interfaces to match the ones within the classes or vice versa.