Skip to content

Commit 2ed8ad2

Browse files
Alejandro GervasioKevinGilmore
authored andcommitted
Inheritance and Composition (Is-a vs Has-a relationship) in Java - BAEL-1598 (eugenp#3871)
* Initial Commit * Fix package names
1 parent 5ce4bd4 commit 2ed8ad2

File tree

16 files changed

+377
-0
lines changed

16 files changed

+377
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.inheritancecomposition.application;
2+
3+
import com.baeldung.inheritancecomposition.model.Actress;
4+
import com.baeldung.inheritancecomposition.model.Computer;
5+
import com.baeldung.inheritancecomposition.model.StandardMemory;
6+
import com.baeldung.inheritancecomposition.model.Person;
7+
import com.baeldung.inheritancecomposition.model.StandardProcessor;
8+
import com.baeldung.inheritancecomposition.model.StandardSoundCard;
9+
import com.baeldung.inheritancecomposition.model.Waitress;
10+
11+
public class Application {
12+
13+
public static void main(String[] args) {
14+
15+
Person person = new Person("John", "[email protected]", 35);
16+
Waitress waitress = new Waitress("Mary", "[email protected]", 22);
17+
System.out.println(waitress.serveStarter("mixed salad"));
18+
System.out.println(waitress.serveMainCourse("steak"));
19+
System.out.println(waitress.serveDessert("cup of cofee"));
20+
Actress actress = new Actress("Susan", "[email protected]", 30);
21+
System.out.println(actress.readScript("Psycho"));
22+
System.out.println(actress.performRole());
23+
Computer computer = new Computer(new StandardProcessor("Intel I3"), new StandardMemory("Kingston", "1TB"));
24+
computer.setSoundCard(new StandardSoundCard("Generic Sound Card"));
25+
System.out.println(computer);
26+
}
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.inheritancecomposition.model;
2+
3+
public class Actress extends Person {
4+
5+
public Actress(String name, String email, int age) {
6+
super(name, email, age);
7+
}
8+
9+
public String readScript(String movie) {
10+
return "Reading the script of " + movie;
11+
}
12+
13+
public String performRole() {
14+
return "Performing a role";
15+
}
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.inheritancecomposition.model;
2+
3+
import java.util.Optional;
4+
5+
public class Computer {
6+
7+
private Processor processor;
8+
private Memory memory;
9+
private SoundCard soundCard;
10+
11+
public Computer(Processor processor, Memory memory) {
12+
this.processor = processor;
13+
this.memory = memory;
14+
}
15+
16+
public void setSoundCard(SoundCard soundCard) {
17+
this.soundCard = soundCard;
18+
}
19+
20+
public Processor getProcessor() {
21+
return processor;
22+
}
23+
24+
public Memory getMemory() {
25+
return memory;
26+
}
27+
28+
public Optional<SoundCard> getSoundCard() {
29+
return Optional.ofNullable(soundCard);
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "Computer{" + "processor=" + processor + ", memory=" + memory + ", soundcard=" + soundCard +"}";
35+
}
36+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.inheritancecomposition.model;
2+
3+
public interface Memory {
4+
5+
String getBrand();
6+
7+
String getSize();
8+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.inheritancecomposition.model;
2+
3+
public class Person {
4+
5+
private final String name;
6+
private final String email;
7+
private final int age;
8+
9+
public Person(String name, String email, int age) {
10+
this.name = name;
11+
this.email = email;
12+
this.age = age;
13+
}
14+
15+
public String getName() {
16+
return name;
17+
}
18+
19+
public String getEmail() {
20+
return email;
21+
}
22+
23+
public int getAge() {
24+
return age;
25+
}
26+
27+
@Override
28+
public String toString() {
29+
return "Person{" + "name=" + name + ", email=" + email + ", age=" + age + "}";
30+
}
31+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.inheritancecomposition.model;
2+
3+
public interface Processor {
4+
5+
String getModel();
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.baeldung.inheritancecomposition.model;
2+
3+
public interface SoundCard {
4+
5+
String getBrand();
6+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.inheritancecomposition.model;
2+
3+
public class StandardMemory implements Memory {
4+
5+
private String brand;
6+
private String size;
7+
8+
public StandardMemory(String brand, String size) {
9+
this.brand = brand;
10+
this.size = size;
11+
}
12+
13+
public String getBrand() {
14+
return brand;
15+
}
16+
17+
public String getSize() {
18+
return size;
19+
}
20+
21+
@Override
22+
public String toString() {
23+
return "Memory{" + "brand=" + brand + ", size=" + size + "}";
24+
}
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.inheritancecomposition.model;
2+
3+
public class StandardProcessor implements Processor {
4+
5+
private String model;
6+
7+
public StandardProcessor(String model) {
8+
this.model = model;
9+
}
10+
11+
@Override
12+
public String getModel() {
13+
return model;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "Processor{" + "model=" + model + "}";
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.inheritancecomposition.model;
2+
3+
public class StandardSoundCard implements SoundCard {
4+
5+
private String brand;
6+
7+
public StandardSoundCard(String brand) {
8+
this.brand = brand;
9+
}
10+
11+
@Override
12+
public String getBrand() {
13+
return brand;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
return "SoundCard{" + "brand=" + brand + "}";
19+
}
20+
}

0 commit comments

Comments
 (0)