Yes but it's clumsy:
@Getter
private IElement button = new ButtonElement(....);
But that will of course make it harder to access some methods of ButtonElement
. So you may want to use either:
private _button = new ButtonElement(....);
@Getter
private IElement button = _button;
i.e. you need two fields, one with the interface and one with the real type.
or
@Getter
private IElement button = new ButtonElement(....);
private ButtonElement button() { return (ButtonElement) button; }
i.e. you now have an internal getter which casts and returns the real type.
Note that Java allows you to define a method which expands the type. So if you have an interface
which says:
IElement getButton();
you can implement this with
public ButtonElement getButton() {...}
Not only will this work but if Java knows which getButton()
you're calling, it will happily give you a ButtonElement
.