public class Solution { public static void main(String[] args) { Rectangle rectangle = new Rectangle(1, 2, 3, 4); System.out.println(getHeight(rectangle.castToHasHeight())); System.out.println(getWidth(rectangle.castToHasWidth())); } public static double getHeight(HasHeight rectangle) { return rectangle.getHeight(); } public static double getWidth(HasWidth rectangle) { return rectangle.getWidth(); } public static class Rectangle { private Point point1; private Point point2; public Rectangle(double x1, double y1, double x2, double y2) { point1 = new Point(x1, y1); point2 = new Point(x2, y2); } public HasHeight castToHasHeight() { class HasHeightImpl implements HasHeight { public double getHeight() { return Math.abs(point1.getY() - point2.getY()); } } return new HasHeightImpl(); } public HasWidth castToHasWidth() { class HasWidthImpl implements HasWidth { public double getWidth() { return Math.abs(point1.getX() - point2.getX()); } } return new HasWidthImpl(); } } }