BAEL-5417 - implements vs extends (#12011)

This commit is contained in:
Parikshit Murria 2022-04-04 15:02:42 -04:00 committed by GitHub
parent f8a69008e1
commit 9759513bf9
15 changed files with 443 additions and 0 deletions

View File

@ -0,0 +1,60 @@
package com.baeldung.implementsvsextends;
import com.baeldung.implementsvsextends.media.model.AudioMedia;
import com.baeldung.implementsvsextends.media.model.Media;
import com.baeldung.implementsvsextends.media.model.VideoMedia;
import com.baeldung.implementsvsextends.media.player.impl.AudioMediaPlayer;
import com.baeldung.implementsvsextends.media.player.impl.CustomMediaPlayer;
import com.baeldung.implementsvsextends.media.player.impl.MultiMediaPlayer;
import com.baeldung.implementsvsextends.media.player.impl.VideoMediaPlayer;
public class Main {
public static void main(String[] args) {
Media media = new Media();
media.setId(001);
media.setTitle("Media1");
media.setArtist("Artist001");
AudioMedia audioMedia = new AudioMedia();
audioMedia.setId(101);
audioMedia.setTitle("Audio1");
audioMedia.setArtist("Artist101");
audioMedia.setBitrate(3500);
audioMedia.setFrequency("256kbps");
VideoMedia videoMedia = new VideoMedia();
videoMedia.setId(201);
videoMedia.setTitle("Video1");
videoMedia.setArtist("Artist201");
videoMedia.setResolution("1024x768");
videoMedia.setAspectRatio("16:9");
System.out.println(media);
System.out.println(audioMedia);
System.out.println(videoMedia);
AudioMediaPlayer audioMediaPlayer = new AudioMediaPlayer();
audioMediaPlayer.play();
audioMediaPlayer.pause();
VideoMediaPlayer videoMediaPlayer = new VideoMediaPlayer();
videoMediaPlayer.play();
videoMediaPlayer.pause();
MultiMediaPlayer multiMediaPlayer = new MultiMediaPlayer();
multiMediaPlayer.play();
multiMediaPlayer.pause();
multiMediaPlayer.seek();
multiMediaPlayer.fastForward();
CustomMediaPlayer customMediaPlayer = new CustomMediaPlayer();
customMediaPlayer.play();
customMediaPlayer.pause();
customMediaPlayer.setId(301);
customMediaPlayer.setTitle("Custom1");
customMediaPlayer.setArtist("Artist301");
System.out.println(customMediaPlayer);
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.implementsvsextends.media.model;
public class AudioMedia extends Media {
private int bitrate;
private String frequency;
@Override
public void printTitle() {
System.out.println("AudioMedia Title");
}
public int getBitrate() {
return bitrate;
}
public void setBitrate(int bitrate) {
this.bitrate = bitrate;
}
public String getFrequency() {
return frequency;
}
public void setFrequency(String frequency) {
this.frequency = frequency;
}
@Override
public String toString() {
return "AudioMedia{" +
"id=" + this.getId() +
", title='" + this.getTitle() + '\'' +
", artist='" + this.getArtist() + '\'' +
", bitrate=" + bitrate +
", frequency='" + frequency + '\'' +
"} ";
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.implementsvsextends.media.model;
public class Media {
private int id;
private String title;
private String artist;
public void printTitle() {
System.out.println("Media Title");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
@Override
public String toString() {
return "Media{" +
"id=" + id +
", title='" + title + '\'' +
", artist='" + artist + '\'' +
'}';
}
}

View File

@ -0,0 +1,35 @@
package com.baeldung.implementsvsextends.media.model;
public class VideoMedia extends Media {
private String resolution;
private String aspectRatio;
public String getResolution() {
return resolution;
}
public void setResolution(String resolution) {
this.resolution = resolution;
}
public String getAspectRatio() {
return aspectRatio;
}
public void setAspectRatio(String aspectRatio) {
this.aspectRatio = aspectRatio;
}
@Override
public String toString() {
return "VideoMedia{" +
"id=" + this.getId() +
", title='" + this.getTitle() + '\'' +
", artist='" + this.getArtist() + '\'' +
"resolution='" + resolution + '\'' +
", aspectRatio='" + aspectRatio + '\'' +
"} ";
}
}

View File

@ -0,0 +1,8 @@
package com.baeldung.implementsvsextends.media.player;
public interface AdvancedPlayerOptions {
void seek();
void fastForward();
}

View File

@ -0,0 +1,8 @@
package com.baeldung.implementsvsextends.media.player;
public interface MediaPlayer {
void play();
void pause();
}

View File

@ -0,0 +1,16 @@
package com.baeldung.implementsvsextends.media.player.impl;
import com.baeldung.implementsvsextends.media.player.MediaPlayer;
public class AudioMediaPlayer implements MediaPlayer {
@Override
public void play() {
System.out.println("AudioMediaPlayer is Playing");
}
@Override
public void pause() {
System.out.println("AudioMediaPlayer is Paused");
}
}

View File

@ -0,0 +1,17 @@
package com.baeldung.implementsvsextends.media.player.impl;
import com.baeldung.implementsvsextends.media.model.Media;
import com.baeldung.implementsvsextends.media.player.MediaPlayer;
public class CustomMediaPlayer extends Media implements MediaPlayer {
@Override
public void play() {
System.out.println("CustomMediaPlayer is Playing");
}
@Override
public void pause() {
System.out.println("CustomMediaPlayer is Paused");
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.implementsvsextends.media.player.impl;
import com.baeldung.implementsvsextends.media.player.AdvancedPlayerOptions;
import com.baeldung.implementsvsextends.media.player.MediaPlayer;
public class MultiMediaPlayer implements MediaPlayer, AdvancedPlayerOptions {
@Override
public void play() {
System.out.println("MultiMediaPlayer is Playing");
}
@Override
public void pause() {
System.out.println("MultiMediaPlayer is Paused");
}
@Override
public void seek() {
System.out.println("MultiMediaPlayer is being seeked");
}
@Override
public void fastForward() {
System.out.println("MultiMediaPlayer is being fast forwarded");
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.implementsvsextends.media.player.impl;
import com.baeldung.implementsvsextends.media.player.MediaPlayer;
public class VideoMediaPlayer implements MediaPlayer {
@Override
public void play() {
System.out.println("VideoMediaPlayer is Playing");
}
@Override
public void pause() {
System.out.println("VideoMediaPlayer is Paused");
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.implementsvsextends.media.model;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
public class AudioMediaUnitTest {
@Test
public void givenAudioMediaInstance_whenCheckedType_thenIsInstanceOfMedia() {
AudioMedia audioMedia = new AudioMedia();
Assert.assertThat(audioMedia, CoreMatchers.<AudioMedia>instanceOf(Media.class));
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.implementsvsextends.media.model;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
public class VideoMediaUnitTest {
@Test
public void givenVideoMediaInstance_whenCheckedType_thenIsInstanceOfMedia() {
VideoMedia videoMedia = new VideoMedia();
Assert.assertThat(videoMedia, CoreMatchers.<VideoMedia>instanceOf(Media.class));
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.implementsvsextends.media.player.impl;
import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class AudioMediaPlayerUnitTest {
private final PrintStream standardOut = System.out;
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
@BeforeEach
public void setUp() {
System.setOut(new PrintStream(outputStreamCaptor));
}
@AfterEach
public void tearDown() {
System.setOut(standardOut);
}
@Test
public void givenAudioMediaPlayer_whenPlay_thenPrintsPlayingString() {
AudioMediaPlayer audioMediaPlayer = new AudioMediaPlayer();
audioMediaPlayer.play();
Assert.assertEquals("AudioMediaPlayer is Playing", outputStreamCaptor.toString()
.trim());
}
@Test
public void givenAudioMediaPlayer_whenPause_thenPrintsPausedString() {
AudioMediaPlayer audioMediaPlayer = new AudioMediaPlayer();
audioMediaPlayer.pause();
Assert.assertEquals("AudioMediaPlayer is Paused", outputStreamCaptor.toString()
.trim());
}
}

View File

@ -0,0 +1,58 @@
package com.baeldung.implementsvsextends.media.player.impl;
import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class MultiMediaPlayerUnitTest {
private final PrintStream standardOut = System.out;
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
@BeforeEach
public void setUp() {
System.setOut(new PrintStream(outputStreamCaptor));
}
@AfterEach
public void tearDown() {
System.setOut(standardOut);
}
@Test
public void givenMultiMediaPlayer_whenPlay_thenPrintsPlayingString() {
MultiMediaPlayer multiMediaPlayer = new MultiMediaPlayer();
multiMediaPlayer.play();
Assert.assertEquals("MultiMediaPlayer is Playing", outputStreamCaptor.toString()
.trim());
}
@Test
public void givenMultiMediaPlayer_whenPause_thenPrintsPausedString() {
MultiMediaPlayer multiMediaPlayer = new MultiMediaPlayer();
multiMediaPlayer.pause();
Assert.assertEquals("MultiMediaPlayer is Paused", outputStreamCaptor.toString()
.trim());
}
@Test
public void givenMultiMediaPlayer_whenSeek_thenPrintsPausedString() {
MultiMediaPlayer multiMediaPlayer = new MultiMediaPlayer();
multiMediaPlayer.seek();
Assert.assertEquals("MultiMediaPlayer is being seeked", outputStreamCaptor.toString()
.trim());
}
@Test
public void givenMultiMediaPlayer_whenFastForward_thenPrintsPausedString() {
MultiMediaPlayer multiMediaPlayer = new MultiMediaPlayer();
multiMediaPlayer.fastForward();
Assert.assertEquals("MultiMediaPlayer is being fast forwarded",
outputStreamCaptor.toString()
.trim());
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.implementsvsextends.media.player.impl;
import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class VideoMediaPlayerUnitTest {
private final PrintStream standardOut = System.out;
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
@BeforeEach
public void setUp() {
System.setOut(new PrintStream(outputStreamCaptor));
}
@AfterEach
public void tearDown() {
System.setOut(standardOut);
}
@Test
public void givenVideoMediaPlayer_whenPlay_thenPrintsPlayingString() {
VideoMediaPlayer videoMediaPlayer = new VideoMediaPlayer();
videoMediaPlayer.play();
Assert.assertEquals("VideoMediaPlayer is Playing", outputStreamCaptor.toString()
.trim());
}
@Test
public void givenVideoMediaPlayer_whenPause_thenPrintsPausedString() {
VideoMediaPlayer videoMediaPlayer = new VideoMediaPlayer();
videoMediaPlayer.pause();
Assert.assertEquals("VideoMediaPlayer is Paused", outputStreamCaptor.toString()
.trim());
}
}