From b25e32479036dd3e0a2d88974598bab5bd7b6867 Mon Sep 17 00:00:00 2001 From: Bhaskar Ghosh Dastidar Date: Sat, 19 Aug 2023 01:06:51 +0530 Subject: [PATCH] [BAEL-6859] Inner Classes vs. Subclasses in Java (#14608) * [BAEL-6859] inner and sub classes * [BAEL-6859] unit test cases added --------- Co-authored-by: Bhaskar --- .../subclassinnerclass/EmailNotifier.java | 17 +++++++++++++++++ .../baeldung/subclassinnerclass/Message.java | 6 ++++++ .../NotificationService.java | 17 +++++++++++++++++ .../baeldung/subclassinnerclass/Notifier.java | 5 +++++ .../TextMessageNotifier.java | 14 ++++++++++++++ .../subclassinnerclass/InnerClassUnitTest.java | 16 ++++++++++++++++ .../subclassinnerclass/SubClassUnitTest.java | 18 ++++++++++++++++++ 7 files changed, 93 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/EmailNotifier.java create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/Message.java create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/NotificationService.java create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/Notifier.java create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/TextMessageNotifier.java create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/subclassinnerclass/InnerClassUnitTest.java create mode 100644 core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/subclassinnerclass/SubClassUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/EmailNotifier.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/EmailNotifier.java new file mode 100644 index 0000000000..1ade675e87 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/EmailNotifier.java @@ -0,0 +1,17 @@ +package com.baeldung.subclassinnerclass; + +import java.util.HashMap; + +public class EmailNotifier extends Notifier { + @Override + void notify(Message e) { + // Provide email specific implementation here + } + + // Inner class for email connection + static class EmailConnector { + private String emailHost; + private int emailPort; + // Getter Setters + } +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/Message.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/Message.java new file mode 100644 index 0000000000..35d751b288 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/Message.java @@ -0,0 +1,6 @@ +package com.baeldung.subclassinnerclass; + +public class Message { + private int message; + // getter setter and other atteibutes +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/NotificationService.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/NotificationService.java new file mode 100644 index 0000000000..2ebbbde961 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/NotificationService.java @@ -0,0 +1,17 @@ +package com.baeldung.subclassinnerclass; + +public class NotificationService { + void notifyMessages() { + // Sending a Text Message + Message textMessage = new Message(); + Notifier textNotifier = new TextMessageNotifier(); + + textNotifier.notify(textMessage); + + // Sending an Email Message + Message emailMessage = new Message(); + Notifier emailNotifier = new EmailNotifier(); + + emailNotifier.notify(emailMessage); + } +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/Notifier.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/Notifier.java new file mode 100644 index 0000000000..aff0372af3 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/Notifier.java @@ -0,0 +1,5 @@ +package com.baeldung.subclassinnerclass; + +public abstract class Notifier { + abstract void notify(Message e); +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/TextMessageNotifier.java b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/TextMessageNotifier.java new file mode 100644 index 0000000000..8b64b4331d --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/main/java/com/baeldung/subclassinnerclass/TextMessageNotifier.java @@ -0,0 +1,14 @@ +package com.baeldung.subclassinnerclass; + +public class TextMessageNotifier extends Notifier { + @Override + void notify(Message e) { + // Provide text message specific implementation here + } + + // Inner class for text message connection + private static class SMSConnector { + private String smsHost; + // Getter Setters + } +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/subclassinnerclass/InnerClassUnitTest.java b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/subclassinnerclass/InnerClassUnitTest.java new file mode 100644 index 0000000000..27577a546f --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/subclassinnerclass/InnerClassUnitTest.java @@ -0,0 +1,16 @@ +package com.baeldung.subclassinnerclass; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import org.junit.Test; + +public class InnerClassUnitTest { + @Test + public void givenInnerStaticClassWhenInstantiatedThenOuterClassIsInstantiated() { + Notifier emailNotifier = new EmailNotifier(); + EmailNotifier.EmailConnector emailConnector = new EmailNotifier.EmailConnector(); + + assertThat(emailNotifier).hasSameClassAs(new EmailNotifier()); + assertThat(emailConnector).isInstanceOf(EmailNotifier.EmailConnector.class); + } +} diff --git a/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/subclassinnerclass/SubClassUnitTest.java b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/subclassinnerclass/SubClassUnitTest.java new file mode 100644 index 0000000000..25e6f5b8a4 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-inheritance/src/test/java/com/baeldung/subclassinnerclass/SubClassUnitTest.java @@ -0,0 +1,18 @@ +package com.baeldung.subclassinnerclass; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import org.junit.Test; + +public class SubClassUnitTest { + @Test + public void givenSubclassWhenInstantiatedThenSubclassObjectIsPossible() { + Notifier emailNotifier = new EmailNotifier(); + assertThat(emailNotifier).hasSameClassAs(new EmailNotifier()); + assertThat(emailNotifier).isExactlyInstanceOf(EmailNotifier.class); + + Notifier textMessageNotifier = new TextMessageNotifier(); + assertThat(textMessageNotifier).isInstanceOf(Notifier.class); + assertThat(textMessageNotifier).isExactlyInstanceOf(TextMessageNotifier.class); + } +}