File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
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

Joined: May 22, 2003
Posts: 657

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

Joined: Jul 10, 2002
Posts: 214

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.


SCJP, SCWCD
Ramdas Sawant
Ranch Hand

Joined: Aug 08, 2005
Posts: 51
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.


If you think you can, you can. If you think you can't, you are right.
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
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

Joined: Aug 28, 2005
Posts: 340
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.


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Joseph Clark
Ranch Hand

Joined: Sep 10, 2005
Posts: 48
See http://java.sun.com/docs/books/jls/third_edition/html/arrays.html , section 10.10 for an explanation.
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Thanks to Sandeep and Mr. Clark
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance