0
class A {}
class B extends A {}

Hi, I'm learning Java and I'm trying to understand why this works:

A[] tab = new A[10];
tab[0] = new B();

and not this:

A[] tab = new B[10];
tab[0] = new A();

this line A[] tab = new B[10]; means that the compiler is booking 10 places for B in memory. tab[0] = new A() is setting tab[0] equal to a new A object that is smaller (?) than B (B extends A).

Why is there an ArrayStoreException: A error ? How does it work ?

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
Alex
  • 2,036
  • 4
  • 22
  • 31
  • 2
    Think about it. If `Dog extends Animal` then dog is an animal, but not all animals are dogs. – Maroun Jun 04 '13 at 09:26

7 Answers7

4

You can store subclasses of an object, inside that object. ie B can be stored in A, but A is a superclass of type B. Basically, any class below class X in the inheritance chain can be referred to as type X, but any class above class X in the inheritance chain can NOT be referred to as type X.

Your example

A[] tab = new A[10]; // Perfectly fine. Equal objects.
tab[0] = new B(); // B is a subclass of A, so this is allowed.

A[] tab = new B[10]; // Same principle as above, B is a subclass of A
tab[0] = new A(); // A is a SUPERCLASS of B, so it can not be referred to as A

In short, X can only be referred to as Y, if X is a subclass of Y. (Or a subclass of a subclass. It's a recursive definition).

Let's use some English terms

Instead of A and B, let's have Item and Book.

public class Item {}

public class Book extends Item {}

Now, it makes sense to say:

Item b = new Book(); // All Books are items.

It does not make sense to say:

Book b = new Item(); // All items are definitely not books.
christopher
  • 26,815
  • 5
  • 55
  • 89
1

According to Documentation

Thrown to indicate that an attempt has been made to store the wrong type of object into an array of objects. For example, the following code generates an ArrayStoreException:

Object x[] = new String[3]; 
x[0] = new Integer(0);
Ravi
  • 30,829
  • 42
  • 119
  • 173
1

"My Animal array will contain only Butterflies (A[] tab = new B[10];). Add a new Animal in the array (tab[0] = new A();)."

How to be sure the inserted Animal is a Butterfly?

sp00m
  • 47,968
  • 31
  • 142
  • 252
1

The thumb rule in java is superclass reference variable can point to sub class object and not vice versa, if you think about it , it makes sense that parent class can point to child class object. List list = new ArrayList()

AJM
  • 129
  • 1
  • 2
  • 7
1

Because if B extends A, B has ALL of the A characteristics (attributes, methods, etc), PLUS his own.

Then each B is also an A, while an A (that would lack the characteristics of B) is NOT a B.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
1

This boils down to the most essential property of a hierarchical type system, which is the Liskov Substitution Principle: wherever an instance of A is allowed, an instance of any of its subtypes is also allowed. Arrays are no special case.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
1

Given:

class A { }

class B extends A { }

We have:

A [] a = new A [10]; // can store A or B objects

A [] b = new B [10]; // can only store B objects
Khaled.K
  • 5,828
  • 1
  • 33
  • 51