OnJava8-Examples/generics/InheritBounds.java

41 lines
1.0 KiB
Java
Raw Permalink Normal View History

2015-09-07 11:44:36 -06:00
// generics/InheritBounds.java
// (c)2021 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.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
class HoldItem<T> {
T item;
HoldItem(T item) { this.item = item; }
T getItem() { return item; }
}
class WithColor2<T extends HasColor>
extends HoldItem<T> {
2016-01-25 18:05:55 -08:00
WithColor2(T item) { super(item); }
2015-06-15 17:47:35 -07:00
java.awt.Color color() { return item.getColor(); }
}
2016-01-25 18:05:55 -08:00
class WithColorCoord2<T extends Coord & HasColor>
extends WithColor2<T> {
WithColorCoord2(T item) { super(item); }
2015-06-15 17:47:35 -07:00
int getX() { return item.x; }
int getY() { return item.y; }
int getZ() { return item.z; }
}
2016-01-25 18:05:55 -08:00
class Solid2<T extends Coord & HasColor & Weight>
extends WithColorCoord2<T> {
2015-06-15 17:47:35 -07:00
Solid2(T item) { super(item); }
int weight() { return item.weight(); }
}
public class InheritBounds {
public static void main(String[] args) {
Solid2<Bounded> solid2 =
new Solid2<>(new Bounded());
solid2.color();
solid2.getY();
solid2.weight();
}
2015-09-07 11:44:36 -06:00
}