[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 <bhaskar.dastidar@freshworks.com>
This commit is contained in:
parent
08b5eda945
commit
b25e324790
|
@ -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
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.subclassinnerclass;
|
||||
|
||||
public class Message {
|
||||
private int message;
|
||||
// getter setter and other atteibutes
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package com.baeldung.subclassinnerclass;
|
||||
|
||||
public abstract class Notifier {
|
||||
abstract void notify(Message e);
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue