From 9759513bf9c8718f28a4ae807adf913fd4bc3b94 Mon Sep 17 00:00:00 2001 From: Parikshit Murria Date: Mon, 4 Apr 2022 15:02:42 -0400 Subject: [PATCH] BAEL-5417 - implements vs extends (#12011) --- .../baeldung/implementsvsextends/Main.java | 60 +++++++++++++++++++ .../media/model/AudioMedia.java | 41 +++++++++++++ .../media/model/Media.java | 47 +++++++++++++++ .../media/model/VideoMedia.java | 35 +++++++++++ .../media/player/AdvancedPlayerOptions.java | 8 +++ .../media/player/MediaPlayer.java | 8 +++ .../media/player/impl/AudioMediaPlayer.java | 16 +++++ .../media/player/impl/CustomMediaPlayer.java | 17 ++++++ .../media/player/impl/MultiMediaPlayer.java | 27 +++++++++ .../media/player/impl/VideoMediaPlayer.java | 16 +++++ .../model/AudioMediaUnitTest.java | 14 +++++ .../model/VideoMediaUnitTest.java | 14 +++++ .../player/impl/AudioMediaPlayerUnitTest.java | 41 +++++++++++++ .../player/impl/MultiMediaPlayerUnitTest.java | 58 ++++++++++++++++++ .../player/impl/VideoMediaPlayerUnitTest.java | 41 +++++++++++++ 15 files changed, 443 insertions(+) create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/Main.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/AudioMedia.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/Media.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/VideoMedia.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/AdvancedPlayerOptions.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/MediaPlayer.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/AudioMediaPlayer.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/CustomMediaPlayer.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/MultiMediaPlayer.java create mode 100644 core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/VideoMediaPlayer.java create mode 100644 core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/model/AudioMediaUnitTest.java create mode 100644 core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/model/VideoMediaUnitTest.java create mode 100644 core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/AudioMediaPlayerUnitTest.java create mode 100644 core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/MultiMediaPlayerUnitTest.java create mode 100644 core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/VideoMediaPlayerUnitTest.java diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/Main.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/Main.java new file mode 100644 index 0000000000..0ebf3c8427 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/Main.java @@ -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); + } +} diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/AudioMedia.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/AudioMedia.java new file mode 100644 index 0000000000..7ede96e83a --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/AudioMedia.java @@ -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 + '\'' + + "} "; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/Media.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/Media.java new file mode 100644 index 0000000000..9b17bf97c8 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/Media.java @@ -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 + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/VideoMedia.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/VideoMedia.java new file mode 100644 index 0000000000..f9affc81e0 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/model/VideoMedia.java @@ -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 + '\'' + + "} "; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/AdvancedPlayerOptions.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/AdvancedPlayerOptions.java new file mode 100644 index 0000000000..a0e58f7d2e --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/AdvancedPlayerOptions.java @@ -0,0 +1,8 @@ +package com.baeldung.implementsvsextends.media.player; + +public interface AdvancedPlayerOptions { + + void seek(); + + void fastForward(); +} diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/MediaPlayer.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/MediaPlayer.java new file mode 100644 index 0000000000..8c5a2f9165 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/MediaPlayer.java @@ -0,0 +1,8 @@ +package com.baeldung.implementsvsextends.media.player; + +public interface MediaPlayer { + + void play(); + + void pause(); +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/AudioMediaPlayer.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/AudioMediaPlayer.java new file mode 100644 index 0000000000..cebde6f0ce --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/AudioMediaPlayer.java @@ -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"); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/CustomMediaPlayer.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/CustomMediaPlayer.java new file mode 100644 index 0000000000..0ff01409f7 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/CustomMediaPlayer.java @@ -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"); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/MultiMediaPlayer.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/MultiMediaPlayer.java new file mode 100644 index 0000000000..e7da8d0298 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/MultiMediaPlayer.java @@ -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"); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/VideoMediaPlayer.java b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/VideoMediaPlayer.java new file mode 100644 index 0000000000..5263f21f9a --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/main/java/com/baeldung/implementsvsextends/media/player/impl/VideoMediaPlayer.java @@ -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"); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/model/AudioMediaUnitTest.java b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/model/AudioMediaUnitTest.java new file mode 100644 index 0000000000..e1e7b6b3f7 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/model/AudioMediaUnitTest.java @@ -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.instanceOf(Media.class)); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/model/VideoMediaUnitTest.java b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/model/VideoMediaUnitTest.java new file mode 100644 index 0000000000..677ed04c40 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/model/VideoMediaUnitTest.java @@ -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.instanceOf(Media.class)); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/AudioMediaPlayerUnitTest.java b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/AudioMediaPlayerUnitTest.java new file mode 100644 index 0000000000..8aee59ecd3 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/AudioMediaPlayerUnitTest.java @@ -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()); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/MultiMediaPlayerUnitTest.java b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/MultiMediaPlayerUnitTest.java new file mode 100644 index 0000000000..558f442276 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/MultiMediaPlayerUnitTest.java @@ -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()); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/VideoMediaPlayerUnitTest.java b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/VideoMediaPlayerUnitTest.java new file mode 100644 index 0000000000..b2c7511323 --- /dev/null +++ b/core-java-modules/core-java-lang-4/src/test/java/com/baeldung/implementsvsextends/player/impl/VideoMediaPlayerUnitTest.java @@ -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()); + } +} \ No newline at end of file