• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Interface

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Silver {
6. public static void main(String []args) {
7. Base[] base = {new Base()};
8. Sub sub[] = new Sub[1];
9. Object obj = base;
10. sub = (Sub[])obj;
11. I1 []i1 = (I1[])obj;
12. }
13. }
Why is it giving runtime error ast line 10. Please explain me in detail.
Thanks
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The object reference obj is pointing to an object of type Base[]. This will throw a ClassCastException.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's because you can't cast arrays of objects.
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Paul Villangca:
I think it's because you can't cast arrays of objects.


Try explaining line 11 Paul ( No pun intended ) :roll:
Peace
Ashish H.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there:
I think it is more of a down cast/up cast issue. While up cast is safe:
11. I1 []i1 = (I1[])obj;
but down cast is problematic at run time:
10. sub = (Sub[])obj;
In both cases JVM is more concered about type
of object obj is pointing to (i.e. Base)
rather than type of obj itself (i.e. Object).
Thanks
Barkat

1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Silver {
6. public static void main(String []args) {
7. Base[] base = {new Base()};
8. Sub sub[] = new Sub[1];
9. Object obj = base;
10. sub = (Sub[])obj;
11. I1 []i1 = (I1[])obj;
12. }
13. }
[ August 23, 2002: Message edited by: Barkat Mardhani ]
 
Paul Villangca
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, my bad. Lucky for me I didn't get array-casting questions in my exam
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The ClassCastException is due to the fact that an object of type Base isn't "castable" to an instance of Sub.
Because Base implements I1 the next cast is valid.
JLS 5.5 Casting conversions explains all. The relevant part is at the end:


TC and RC are reference types and type RC can be cast to TC by a recursive application of these run-time rules for casting.


That is, applying recursively we must apply:


If T is a class type, then R must be either the same class (�4.3.4) as T or a subclass of T, or a run-time exception is thrown.


Because Base is parent of Sub this check fails.
Applying recursively again, this suffice for the last cast to be ok:


If T is an interface type, then R must implement (�8.1.4) interface T, or a run-time exception is thrown.

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this will explain this a bit more
1. base is variable that points to a array of Base objects.
2. obj = base now make obj point to this array of Base objects.
So we cannot cast obj to an array of Sub. The array members may be of type Sub but the array is of type Base
Look at the following code it explains thing a bit more

[ August 23, 2002: Message edited by: Rahul Phadnis ]
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface I1 {}
interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
public class Silver{
public static void main(String []args) {
//Base[] base = {new Base()}; // line 1
Base[] base = {new Sub()}; //line 2
Sub sub[] = new Sub[1];
Object obj = base;
if (obj instanceof Sub[]) {
sub = (Sub[]) obj;
System.out.println("Sub Cast done");
} else if (obj instanceof Base[]) {
Base[] newbase = (Base[]) obj;
System.out.println("Base Cast done");
} else {
System.out.println("No Cast done");

}
I1 []i1 = (I1[])obj;
System.out.println("Trying to cast array member now");
Object o1 = base[0]; //make o1 point to the 1 st member of the array
if ( o1 instanceof Sub) {
Sub castedsub = (Sub) o1;
System.out.println("Cast valid for member");
} else {
System.out.println("Not valid cast");
}
}
}
Actual code was with line 1 when u explained you have explained with line 2. but in both cases output is same what you were expecting.

My understanding was in Class Hierarchy you have Object at the highest level.
My question is
If Base class can make use of Object class why not Sub class?
Is it because Sub class object refrence is array ?
If the Sub class defined as
class Sub {
Sub s = new Sub();
Object obj = s;
}
Can I say like that?

From your explanation what I understood was you cannot cast array to object. Am I right?
Thanks.
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Suresh:
I will try one more time to give you my reasoning
of why you are getting run time error. I am going to
add comments to your original code to do that:
1. interface I1 {}
2. interface I2 {}
3. class Base implements I1 {}
4. class Sub extends Base implements I2 {}
5. class Silver {
6. public static void main(String []args) {
7. Base[] base = {new Base()};
/** above statement creates a new reference object. Its type is array. However, the type
of objects this reference object (i.e. base) is
pointing to is Base type.
**/
8. Sub sub[] = new Sub[1];
/** type of sub (a reference object) is array and
objects it is pointing to are of type Sub.
**/
9. Object obj = base;
/** Here you are creating a new object obj. You
are saying obj is going to point to same objects
that base is pointing to (i.e. Base types) but
type of obj itself (the reference object) is not going to be same as type of
base (i.e. an array). Instead, it is type Object.
As array type is low in class hierarchy, it
is an up-cast. Remember, up-cast are no problems
at compile or run time. The important point to
note here is that we are NOT up-casting the
referred object type (i.e. Base) to Object. It is
simply a new object obj (a reference object) with
its own type as Object but it is poinint to same
objects base is pointing to.
**/
10. sub = (Sub[])obj;
/** In this statement, you are saying: take the
objects obj is pointing to (i.e. Base types) and
cast them to Sub types. This is down-cast. Now
down-cast is not allowed by compiler unless it
is explicit, which it is in this case. Therefore, you
did not get compile time error. However, at run
time JVM finds that objects obj is pointing to
are STILL of type Base and they can not be casted
down to Sub types. Therefore, a run-time error.
**/
11. I1 []i1 = (I1[])obj;
/** in this statement, you saying: take the objects obj is pointing to (i.e. Base type objects)
and cast them to I1 type. This is an up-cast. And
it is allowed by compiler and JVM.
**/
Thanks
Barkat
12. }
13. }
[ August 23, 2002: Message edited by: Barkat Mardhani ]
 
suresh kamsa
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barkat.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic