Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

ArrayStoreException - Inquition doubt

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I got the above question from Inquisition.

While i compile the code above I got error
Exception in thread "main" java.lang.ArrayStoreException: Car
at Ranch1.evilMethod(Ranch1.java:44)
at Ranch1.main(Ranch1.java:41)
type of ar[0] in evilMethod is Transport and Car IS A Transport, they why we can't store Car object into ar[0]?

Thanks in advance for clarifications.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An array can contain object of its type or subtype, anything else will throw a runtime exception ArrayStoreException.
At runtime ar is refering the Plain array and car object is being stored in object of type Plane.
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Kumar Singh:
An array can contain object of its type or subtype, anything else will throw a runtime exception ArrayStoreException.
At runtime ar is refering the Plain array and car object is being stored in object of type Plane.



While determining the type of an array, should we consider compile time or runtime?

i.e., in evilMethod() argument ar is declared as Transport, so why can't we store Car object inside ar (since Car IS A Transport)

Please correct my understanding is wrong !!!
[ December 22, 2006: Message edited by: raja kanak ]
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally postd by raja

i.e., in evilMethod() argument ar is declared as Transport, so why can't we store Car object inside ar (since Car IS A Transport)


That is why compiler has allowed you to store an objecto of car in array of type Transport.
But thing about the runtime ar is actually refering to a plain array.
Due to thing type of mismash generics has been intoduced in Java5.
 
Who among you feels worthy enough to be my best friend? Test 1 is to read this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic