// Bounded type parameter example class NumberContainer<T extends Number> { private T number; public NumberContainer(T number) { this.number = number; } public T getNumber() { return number; } public void setNumber(T number) { this.number = number; } public void display() { System.out.println("Number: " + number); } } // Wildcard argument example class Utils { // Method to display elements of a NumberContainer with wildcard argument public static void displayNumberContainer(NumberContainer<?> container) { System.out.println("Number in container: " + container.getNumber()); } } public class Main { public static void main(String[] args) { // Bounded type parameter example NumberContainer<Integer> intContainer = new NumberContainer<>(10); intContainer.display(); NumberContainer<Double> doubleContainer = new NumberContainer<>(3.14); doubleContainer.display(); // Wildcard argument example Utils.displayNumberContainer(intContainer); Utils.displayNumberContainer(doubleContainer); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter