Please help me grasp some concepts that might have something to do with (at least) the inheritance mechanisms. Consider the following situation: after creating a class A (with 3 instance variables and 5 methods, including the constructor), another class, B, is created, extending A. This new class has one additional instance variable, two additional methods, and a method overriding one of the As methods. When an A[] array is declared, it appears that it can hold objects of type B, but a B[] array cannot hold objects of type A. Also, both type of objects can be stored in an Object[] array, given that both A and B extend (by default, as far as I know) the Object class (with its 11 own distinct methods) but none of A[] or B[] can store Object objects. What I cannot understand so far is why a smaller (at least apparently) location can hold a bigger relative object, while the opposite is not possible. I am sure that Im missing (or confusing) something but what is that?
when you declare an array A[] the type is of class A. B extends A. Casting type B to Type A is admissible since all the methods of A are existing in B and calling any method of A on B would be allowed. But if you put A in B[] then they type is of class B, which has more methods than A.If using B's type you access a method which doesn't exist in A, that wouldn't be allowed. Hence the restriction of putting objects of class A in array of B[]. Same argument is applicable to 'Object' objects being put in either A[] or B[].