Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance

 
Ritu Kapoor
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Edwin Keeton
Ranch Hand
Posts: 214
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
R Sawant
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • 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.
 
Joseph Clark
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks to Sandeep and Mr. Clark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic