Inheritance and Composition (Is-a vs Has-a relationship) in Java - BAEL-1598 (#3871)
* Initial Commit * Fix package names
This commit is contained in:
parent
5ce4bd458a
commit
2ed8ad2e00
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.inheritancecomposition.application;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.Actress;
|
||||
import com.baeldung.inheritancecomposition.model.Computer;
|
||||
import com.baeldung.inheritancecomposition.model.StandardMemory;
|
||||
import com.baeldung.inheritancecomposition.model.Person;
|
||||
import com.baeldung.inheritancecomposition.model.StandardProcessor;
|
||||
import com.baeldung.inheritancecomposition.model.StandardSoundCard;
|
||||
import com.baeldung.inheritancecomposition.model.Waitress;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
Person person = new Person("John", "john@domain.com", 35);
|
||||
Waitress waitress = new Waitress("Mary", "mary@domain.com", 22);
|
||||
System.out.println(waitress.serveStarter("mixed salad"));
|
||||
System.out.println(waitress.serveMainCourse("steak"));
|
||||
System.out.println(waitress.serveDessert("cup of cofee"));
|
||||
Actress actress = new Actress("Susan", "susan@domain.com", 30);
|
||||
System.out.println(actress.readScript("Psycho"));
|
||||
System.out.println(actress.performRole());
|
||||
Computer computer = new Computer(new StandardProcessor("Intel I3"), new StandardMemory("Kingston", "1TB"));
|
||||
computer.setSoundCard(new StandardSoundCard("Generic Sound Card"));
|
||||
System.out.println(computer);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class Actress extends Person {
|
||||
|
||||
public Actress(String name, String email, int age) {
|
||||
super(name, email, age);
|
||||
}
|
||||
|
||||
public String readScript(String movie) {
|
||||
return "Reading the script of " + movie;
|
||||
}
|
||||
|
||||
public String performRole() {
|
||||
return "Performing a role";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class Computer {
|
||||
|
||||
private Processor processor;
|
||||
private Memory memory;
|
||||
private SoundCard soundCard;
|
||||
|
||||
public Computer(Processor processor, Memory memory) {
|
||||
this.processor = processor;
|
||||
this.memory = memory;
|
||||
}
|
||||
|
||||
public void setSoundCard(SoundCard soundCard) {
|
||||
this.soundCard = soundCard;
|
||||
}
|
||||
|
||||
public Processor getProcessor() {
|
||||
return processor;
|
||||
}
|
||||
|
||||
public Memory getMemory() {
|
||||
return memory;
|
||||
}
|
||||
|
||||
public Optional<SoundCard> getSoundCard() {
|
||||
return Optional.ofNullable(soundCard);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Computer{" + "processor=" + processor + ", memory=" + memory + ", soundcard=" + soundCard +"}";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public interface Memory {
|
||||
|
||||
String getBrand();
|
||||
|
||||
String getSize();
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final String name;
|
||||
private final String email;
|
||||
private final int age;
|
||||
|
||||
public Person(String name, String email, int age) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Person{" + "name=" + name + ", email=" + email + ", age=" + age + "}";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public interface Processor {
|
||||
|
||||
String getModel();
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public interface SoundCard {
|
||||
|
||||
String getBrand();
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class StandardMemory implements Memory {
|
||||
|
||||
private String brand;
|
||||
private String size;
|
||||
|
||||
public StandardMemory(String brand, String size) {
|
||||
this.brand = brand;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public String getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Memory{" + "brand=" + brand + ", size=" + size + "}";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class StandardProcessor implements Processor {
|
||||
|
||||
private String model;
|
||||
|
||||
public StandardProcessor(String model) {
|
||||
this.model = model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getModel() {
|
||||
return model;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Processor{" + "model=" + model + "}";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class StandardSoundCard implements SoundCard {
|
||||
|
||||
private String brand;
|
||||
|
||||
public StandardSoundCard(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SoundCard{" + "brand=" + brand + "}";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.inheritancecomposition.model;
|
||||
|
||||
public class Waitress extends Person {
|
||||
|
||||
public Waitress(String name, String email, int age) {
|
||||
super(name, email, age);
|
||||
}
|
||||
|
||||
public String serveStarter(String starter) {
|
||||
return "Serving a " + starter;
|
||||
}
|
||||
|
||||
public String serveMainCourse(String mainCourse) {
|
||||
return "Serving a " + mainCourse;
|
||||
}
|
||||
|
||||
public String serveDessert(String dessert) {
|
||||
return "Serving a " + dessert;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.inheritancecomposition.test;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.Actress;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class ActressUnitTest {
|
||||
|
||||
private static Actress actress;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpActressInstance() {
|
||||
actress = new Actress("Susan", "susan@domain.com", 30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCalledgetName_thenEqual() {
|
||||
assertThat(actress.getName()).isEqualTo("Susan");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCalledgetEmail_thenEqual() {
|
||||
assertThat(actress.getEmail()).isEqualTo("susan@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCalledgetAge_thenEqual() {
|
||||
assertThat(actress.getAge()).isEqualTo(30);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCalledreadScript_thenEqual() {
|
||||
assertThat(actress.readScript("Psycho")).isEqualTo("Reading the script of Psycho");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCalledperfomRole_thenEqual() {
|
||||
assertThat(actress.performRole()).isEqualTo("Performing a role");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package com.baeldung.inheritancecomposition.test;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.Computer;
|
||||
import com.baeldung.inheritancecomposition.model.Memory;
|
||||
import com.baeldung.inheritancecomposition.model.Processor;
|
||||
import com.baeldung.inheritancecomposition.model.StandardMemory;
|
||||
import com.baeldung.inheritancecomposition.model.StandardProcessor;
|
||||
import com.baeldung.inheritancecomposition.model.StandardSoundCard;
|
||||
import java.util.Optional;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CompositionUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void givenComputerInstance_whenExtractedEachField_thenThreeAssertions() {
|
||||
Computer computer = new Computer(new StandardProcessor("Intel I3"), new StandardMemory("Kingston", "1TB"));
|
||||
computer.setSoundCard(new StandardSoundCard("Generic Sound Card"));
|
||||
assertThat(computer.getProcessor()).isInstanceOf(Processor.class);
|
||||
assertThat(computer.getMemory()).isInstanceOf(Memory.class);
|
||||
assertThat(computer.getSoundCard()).isInstanceOf(Optional.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.inheritancecomposition.test;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.Actress;
|
||||
import com.baeldung.inheritancecomposition.model.Person;
|
||||
import com.baeldung.inheritancecomposition.model.Waitress;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class InheritanceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCheckedType_thenIsInstanceOfPerson() {
|
||||
assertThat(new Waitress("Mary", "mary@domain.com", 22)).isInstanceOf(Person.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenActressInstance_whenCheckedType_thenIsInstanceOfPerson() {
|
||||
assertThat(new Actress("Susan", "susan@domain.com", 30)).isInstanceOf(Person.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
package com.baeldung.inheritancecomposition.test;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.Person;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class PersonUnitTest {
|
||||
|
||||
private static Person person;
|
||||
|
||||
@BeforeClass
|
||||
public static void setPersonInstance() {
|
||||
person = new Person("John", "john@domain.com", 35);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonInstance_whenCalledgetName_thenEqual() {
|
||||
assertThat(person.getName()).isEqualTo("John");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonInstance_whenCalledgetEmail_thenEqual() {
|
||||
assertThat(person.getEmail()).isEqualTo("john@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPersonInstance_whenCalledgetAge_thenEqual() {
|
||||
assertThat(person.getAge()).isEqualTo(35);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.inheritancecomposition.test;
|
||||
|
||||
import com.baeldung.inheritancecomposition.model.Waitress;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
public class WaitressUnitTest {
|
||||
|
||||
private static Waitress waitress;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpWaitressInstance() {
|
||||
waitress = new Waitress("Mary", "mary@domain.com", 22);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledgetName_thenOneAssertion() {
|
||||
assertThat(waitress.getName()).isEqualTo("Mary");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledgetEmail_thenOneAssertion() {
|
||||
assertThat(waitress.getEmail()).isEqualTo("mary@domain.com");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledgetAge_thenOneAssertion() {
|
||||
assertThat(waitress.getAge()).isEqualTo(22);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledserveStarter_thenOneAssertion() {
|
||||
assertThat(waitress.serveStarter("mixed salad")).isEqualTo("Serving a mixed salad");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledserveMainCourse_thenOneAssertion() {
|
||||
assertThat(waitress.serveMainCourse("steak")).isEqualTo("Serving a steak");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWaitressInstance_whenCalledserveDessert_thenOneAssertion() {
|
||||
assertThat(waitress.serveDessert("cup of coffee")).isEqualTo("Serving a cup of coffee");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue