• 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

Inheritance

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code
-------------------------------------
class Parent{int i = 10;}

class Child extends Parent{ int k = 12;}

public class Class15 {
public static void main(String args[]){
Child [] c = new Child[2];
Parent [] p = c;

p[0] = new Parent(); //1
System.out.println("Value : "+p[0].i); //2
}
}
---------------------------------------------------------------

Query 1: Why am I getting Runtime exception at line //2 when I'm reinitiallizing Parent at //1.

Please explain this.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So you would get a NullPointerException when you run this code.

No, there's no NPE here. The problem arises when trying to add a new Parent object to a Child array. The confusion comes from the Child[] array reference being type compatible with the Parent[] array. The underlying array remains of the Child[] type, so adding a Parent object will cause an assignment error.
 
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect you're actually getting the exception at //1 when you attempt to store a Parent type into an array of Child.

Remember that elements of an array must be type-compatible with the type of the array. A Child is-a Parent, but a Parent is not a Child.
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritu,
Edwin had made a good point.Remember that elements of an array must be type-compatible with the type of the array. To clear his point see the following code

Object o[] = new String[2];
o[0] = new Integer(143);

The above code will compile fine but will throw an ArrayStoreException at runtime.
Hope this will clear ur doubt.

Regards,
Ramdas.
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

My confusion is whats wrong happening at //1 that we are getting ArrayStoreException. What I feel is that we are just reinitializing the array element.

Please elaborate on it.

Thanks
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ritu Kapoor:
Hi All,

My confusion is whats wrong happening at //1 that we are getting ArrayStoreException. What I feel is that we are just reinitializing the array element.

Please elaborate on it.

Thanks



Hi,

p[0] = new Parent(); //1
You are right that here you are just reinitialiing the array element.

But when you did this :
Child [] c = new Child[2];
Parent [] p = c;

I think now p refers an array that can only contain Child elements. So doing p[0]=new Parent(); means trying to add a Parent object in an array that can only hold Child objects which is not possible, hence we get ArrayStoreException.

Correct me if I am wrong.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See http://java.sun.com/docs/books/jls/third_edition/html/arrays.html , section 10.10 for an explanation.
 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Sandeep and Mr. Clark
 
reply
    Bookmark Topic Watch Topic
  • New Topic