22 lines
500 B
Plaintext
22 lines
500 B
Plaintext
![]() |
Combine inheritance and array covariance, and the results can potentially get nasty. Together, they allow you to write array stores that will compile, but cause errors at runtime.
|
||
|
|
||
|
|
||
|
== Noncompliant Code Example
|
||
|
|
||
|
----
|
||
|
public class Bowl {
|
||
|
|
||
|
abstract class Fruit { }
|
||
|
class Apple : Fruit { }
|
||
|
class Orange : Fruit { }
|
||
|
|
||
|
Fruit [] fruits;
|
||
|
|
||
|
public void fillBowl(){
|
||
|
fruits = new Apple[30];
|
||
|
fruits[0] = new Orange(); // Noncompliant; this compiles but raises exception at runtime
|
||
|
}
|
||
|
}
|
||
|
----
|
||
|
|