// generics/BasicBounds.java // (c)2017 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. interface HasColor { java.awt.Color getColor(); } class WithColor { T item; WithColor(T item) { this.item = item; } T getItem() { return item; } // The bound allows you to call a method: java.awt.Color color() { return item.getColor(); } } class Coord { public int x, y, z; } // This fails. Class must be first, then interfaces: // class WithColorCoord { // Multiple bounds: class WithColorCoord { T item; WithColorCoord(T item) { this.item = item; } T getItem() { return item; } java.awt.Color color() { return item.getColor(); } int getX() { return item.x; } int getY() { return item.y; } int getZ() { return item.z; } } interface Weight { int weight(); } // As with inheritance, you can have only one // concrete class but multiple interfaces: class Solid { T item; Solid(T item) { this.item = item; } T getItem() { return item; } java.awt.Color color() { return item.getColor(); } int getX() { return item.x; } int getY() { return item.y; } int getZ() { return item.z; } int weight() { return item.weight(); } } class Bounded extends Coord implements HasColor, Weight { @Override public java.awt.Color getColor() { return null; } @Override public int weight() { return 0; } } public class BasicBounds { public static void main(String[] args) { Solid solid = new Solid<>(new Bounded()); solid.color(); solid.getY(); solid.weight(); } }