• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote the following program:


In the above program, what is the array type for array a. Is it animal or dog. Please explain. I thought it would be animal and the program would run fine. However, it is giving me ArrayStoreExcpetion.

[ Jesper Young: Corrected use of code tags ]
[ November 04, 2008: Message edited by: Jesper Young ]
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a's type is Animal[]. As to the rest I do not have a good answer.
 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that

animal[] a = new dog[3];

you created dog array object which is referenced by animal a.

a[0]= new animal();

it is object of type dog then how can you put animal in dog?

dog is an animal but not vice-versa.

correct me if i am wrong.....
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi garima,

the reason you are getting an ArrayStoreException is because you are trying to add an Animal into a Dog array.

Note:
You can add a subtype(Dog) object to an array of the supertype(Animal),not vice versa.

if you change the main method as



the code now compiles fine.
Hope you get the point
 
pradeepta chopra
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
correct Ganeshkumar
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Ganesh explains, the program will create an array of type dog, and this array will be refenced using animal refernce. Anyway, the objects that can be stored in the array should be of type dog.

So that will throw an exception, if you try to add an animal object to the array
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about this code

Parent[] p = {new Dog(), new Cat()};

Here p is of type Parent. Am i right.
 
garima jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry for the typo

It should be
animal[] p = {new dog(), new cat()};
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
animal[] p = {new dog(), new cat()};

Here p is of type animal...
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

But P is just a refence of type animal right?
It is not the actula array or object.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
p is a array reference which can store objects of type animal and it's sub types.....
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am still not clear on this...?
 
Madan Mohan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for example : here also we are doing the same

class Animal {
public void eat() {
System.out.println("Animal");
}
}
class Dog extends Animal {

public void eat() {
System.out.println("Dog");
}
}
class DemoClass{

public static void main(String args[]){

Animal dog = new Dog();
dog = new Animal();
dog.eat(); // <--- here

}

}
but this comiles fine...then why not in the case of array...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to distinguish the array and the elements of the array. A dog array IS-A animal array, and hence, can be referred to by an animal array reference. However, it is still a dog array, that is being referred to, whose elements must be objects that IS-A dog type.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic