OnJava8-Examples/reuse/PlaceSetting.java

83 lines
1.6 KiB
Java
Raw Normal View History

2015-09-07 11:44:36 -06:00
// reuse/PlaceSetting.java
2015-12-15 11:47:04 -08:00
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2016-01-25 18:05:55 -08:00
// Combining composition & inheritance
2015-06-15 17:47:35 -07:00
class Plate {
Plate(int i) {
2015-11-03 12:00:44 -08:00
System.out.println("Plate constructor");
2015-06-15 17:47:35 -07:00
}
}
class DinnerPlate extends Plate {
DinnerPlate(int i) {
super(i);
2015-11-03 12:00:44 -08:00
System.out.println("DinnerPlate constructor");
2015-06-15 17:47:35 -07:00
}
}
class Utensil {
Utensil(int i) {
2015-11-03 12:00:44 -08:00
System.out.println("Utensil constructor");
2015-06-15 17:47:35 -07:00
}
}
class Spoon extends Utensil {
Spoon(int i) {
super(i);
2015-11-03 12:00:44 -08:00
System.out.println("Spoon constructor");
2015-06-15 17:47:35 -07:00
}
}
class Fork extends Utensil {
Fork(int i) {
super(i);
2015-11-03 12:00:44 -08:00
System.out.println("Fork constructor");
2015-06-15 17:47:35 -07:00
}
}
class Knife extends Utensil {
Knife(int i) {
super(i);
2015-11-03 12:00:44 -08:00
System.out.println("Knife constructor");
2015-06-15 17:47:35 -07:00
}
}
// A cultural way of doing something:
class Custom {
Custom(int i) {
2015-11-03 12:00:44 -08:00
System.out.println("Custom constructor");
2015-06-15 17:47:35 -07:00
}
}
public class PlaceSetting extends Custom {
private Spoon sp;
private Fork frk;
private Knife kn;
private DinnerPlate pl;
public PlaceSetting(int i) {
super(i + 1);
sp = new Spoon(i + 2);
frk = new Fork(i + 3);
kn = new Knife(i + 4);
pl = new DinnerPlate(i + 5);
2015-11-03 12:00:44 -08:00
System.out.println("PlaceSetting constructor");
2015-06-15 17:47:35 -07:00
}
public static void main(String[] args) {
PlaceSetting x = new PlaceSetting(9);
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
Custom constructor
Utensil constructor
Spoon constructor
Utensil constructor
Fork constructor
Utensil constructor
Knife constructor
Plate constructor
DinnerPlate constructor
PlaceSetting constructor
2015-09-07 11:44:36 -06:00
*/