23 lines
514 B
Plaintext
23 lines
514 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
|
|
|
|
[source,text]
|
|
----
|
|
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
|
|
}
|
|
}
|
|
----
|
|
|