Storing same type but still getting an ArrayStoreException
<p>I have strange problem. I have three classes. Book ( abstract ), and two subclasses ( Fiction, Nonfiction). I have created array of  Book references. That array can hold both subclasse's objects. I'have following code for Fiction class.</p>

<pre><code>public class Fiction extends Book{

public Fiction(String title) {
    super(title);

    setPrice();
}

public void setPrice() {

    super.price = 24.99;

}
</code></pre>

<p>}</p>

<p>And the "same" code for Nonfiction class</p>

<pre><code>public class Nonfiction extends Book{

public Nonfiction(String title) {
    super(title);

    setPrice();
}

public void setPrice() {

    super.price = 37.99;

}
</code></pre>

<p>}</p>

<p>And this is Main program.</p>

<pre><code>import javax.swing.JOptionPane;
</code></pre>

<p>public class Main {</p>

<p>public static void main(String[] args) {</p>

<pre><code>    Book[] book = new Fiction[6];

    for(int i = 0; i&lt;book.length; i++) {

        if(i&lt;4) {
            String title = JOptionPane.showInputDialog("Enter fiction book, no "+i);
            book[i] = new Fiction(title);

        } else {

            String title = JOptionPane.showInputDialog("Enter non fiction book, no "+i);
            book[i] = new Nonfiction(title);

        }

    }

    for(int x = 0; x&lt;book.length; x++) {

        System.out.println("Book named "+book[x].getTitle()+" costs "+book[x].getPrice());

    }


}
</code></pre>

<p>}</p>

<p>For any reason i'm getting ArrayStoreException on this line..</p>

<pre><code>book[i] = new Nonfiction(title);
</code></pre>

<p>But same code works for Fiction class. I checked java documentation and it says that this exception is thrown when i'm trying to store different objects in array ( like String and Int ), but in this case they are all the same objects. Both classes are children of superclass, that means, they are of the same type. I'm confsued. A little help here please?</p>
