<< §1.3.1.(j) Overriding and compatibility | ↑ Table of Contents ↑ |
§1.3.1.(k) Covariant return types
Given a team T1
with two roles R1
and R2
where R2
explicitly inherits from R1
, both roles defining
a method m
returning some type A
.
Given also a sub-team of T1
, T2
, where T2.R1
overrides m
with a covariant return type B
(sub-type of A
):
public team class T1 {
protected abstract class R1 {
abstract A m();
}
protected class R2 extends R1 {
A m() { return new A(); }
}
}
public team class T2 extends T1 {
protected class R1 {
@Override B m() { return new B(); } // this declaration renders class T2.R2 illegal
}
}
In this situation role T2.R2
will be illegal unless also overriding m
with a return type that is at least B
.
Note, that the actual error occurs at the implicitly inherited method T2.R2.m
which is not visible in the source code,
even T2.R2
need not be mentioned explicitly in the source code.
A compiler should flag this as an imcompatibility at the team level, because a team must specialize inherited roles
in a consistent way.
<< §1.3.1.(j) Overriding and compatibility | ↑ Table of Contents ↑ |