Inheritance and Composition (Is-a vs Has-a relationship) in Java - BAEL-1598 (#3871)

* Initial Commit

* Fix package names
This commit is contained in:
Alejandro Gervasio
2018-04-01 22:50:12 -03:00
committed by KevinGilmore
parent 5ce4bd458a
commit 2ed8ad2e00
16 changed files with 377 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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";
}
}

View File

@@ -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 +"}";
}
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.inheritancecomposition.model;
public interface Memory {
String getBrand();
String getSize();
}

View File

@@ -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 + "}";
}
}

View File

@@ -0,0 +1,6 @@
package com.baeldung.inheritancecomposition.model;
public interface Processor {
String getModel();
}

View File

@@ -0,0 +1,6 @@
package com.baeldung.inheritancecomposition.model;
public interface SoundCard {
String getBrand();
}

View File

@@ -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 + "}";
}
}

View File

@@ -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 + "}";
}
}

View File

@@ -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 + "}";
}
}

View File

@@ -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;
}
}