Polymorphism in Java Arrays

Wednesday, December 24, 2008

When I was preparing for my SCJP 6.0,I was learning many interesting features of java, one such thing was how polymorphism would apply to java arrays as compared to java objects.

For a customary introduction, polymorphism is a OOPS concept which allows a class to hold a reference to an instance of any of its subclasses and invoke methods of different subclass objects to which the reference points to at runtime, in other words this is also called runtime ploymorphism in the sense the object on which the method is invoked is determined at runtime, of course the subclass should override the method which the superclass defines and those are the only methods which can be invoked through the superclass reference to the subclass instance. Ok, enough on polymorphism.

I was discussing how polymorphism would work with arrays with one of my friend (in fact I attribute this entry to him as I learnt this through his fancy questions), the scenario is like this.

1. Declare an array of type baseclass which points to the subclass array object (Polymorphism or polymorphic reference)
2. Assign the first array element of baseclass to the subclass object (Not a great deal of thinking required to tell that its allowed).
3. Assign the second array element of baseclass to the baseclass object.

Now we are interested in how the behavior would be on the assignment in line 3, lets go through the code.

Listing 1: subClass.java

1: /**
 2:  * @(#)subClass.java
 3:  *
 4:  *
 5:  * @author: Prasanna S 
 6:  * @version 1.00 2008/11/25
 7:  */
8:
9: class baseClass {
10:         public baseClass() {    
11:         }
12: }
13:
14: public class subClass extends baseClass {
15:
16:     public subClass() {
17:     }
18:
19:     public static void main (String args[]) {
20: 
21:         // Creating a baseClass Array reference to point to subClass Array object - OK
22:         baseClass baseClassArray [] = new subClass [5];
23: 
24:         // Assigning a baseClass Array element to subClass instance - OK
25:         baseClassArray[0] = new subClass();
26: 
27:         // Assigning a baseClass Array element to baseClass instance 
28:         // Runtime exception - ArrayStoreException
29:         
30:         baseClassArray[1] = new baseClass();
31:     }
32: }
Output and Explanation:

As commented out in the code, the above program will compile successfully, but will throw a runtime exception at line 30 as shown below.

Exception in thread "main" java.lang.ArrayStoreException: baseClass
at subClass.main(subClass.java:30)

There are two classes named baseClass and subClass in the above listing, and in line 22, we create a base class array reference to point to a subclass object array reference.

Line 25 assigns a subClass object to baseClassArray[0] which is acceptable according to polymorphism rules.

Line 30 assigns a baseClass object to baseClassArray[0], which results in ArrayStoreException because you can't have a reference to a baseClass object in an subClass array object NEVER, note that its very easy to get confused and accept that this code would work if one sees the reference type in the left hand side of the assignment.

The lesson: If we try to store a baseClass reference in an object of subClass type, we will get a ClassCastException for java objects as it violates polymorphism principle or IS-A relationship (a subclass IS-A baseclass through inheritance, but not otherwise), but for arrays you will get an ArrayStoreException.

1 comment:

Richard Lawson said...

Nice post, I just ran across this bug in production code today. Since it is a runtime bug it only happened recently. Your post helped me diagnose it,thanks !


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.