• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

casting

 
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,

interface I1 {} interface I2 {}
class Base implements I1 {}
class Sub extends Base implements I2 {}
class Silver {
public static void main(String []args) {
Base[] base = {new Base()};
Sub sub[] = new Sub[1]; // 1
Object obj = base; // 2
sub = (Sub[])obj; // 3
I1 []i1 = (I1[])obj; // 4
}}

The answer is that,there is a runtime error at line 3(This I understood)
BUT.....why there is no runtime error at line 4.

Can anybody please explain me......

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


Line 3 gives run-time error because you are trying to assign obj which has base contents to sub object.

On line 4, you are assigning obj (which contains base object) to i1 which is of type I1.
However, base implements I1 so no runtime error is generated.

Hope that helps you.
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay,I am not able to understand your explanation......

I understood,why line 3 is giving a runtime error.....but why line 4 is NOT giving a runtime error?

thanks
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jay,I am not able to understand your explanation......

I understood,why line 3 is giving a runtime error.....but why line 4 is NOT giving a runtime error?

thanks
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here we go:

I1 I2
| |
Base |
|__ __|
Sub

Just draw the hierarchy of the class/interface.

Sub sub[] = new Sub[1]; // 1
Object obj = base; // 2

sub = (Sub[])obj; // 3 Here at runtime obj refers to base objects and is assigned to object of type sub which is invalid. So runtime exception is thrown and u know it.

I1 []i1 = (I1[])obj; // 4 Here at runtime obj refers to base object and is assigned to object of type I1 which is parent of base. Definitely this is valid at runtime.

Hope this clears
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anand.....It's clear to me now.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Puja,
Didn't mean to confuse you.. . I was in a rush to head home. Here is the link which explains casting with nice diagrams.
Click here
[ January 20, 2005: Message edited by: Jay Pawar ]
 
Puja S
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jay for the link.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guyz im new to java and this board.

i have a question in line 4

since we are passing the derived class reference(base) to the super class ( I1 interface in our case) shoud we have to explicty type cast it .
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anand Koppal:
I1 []i1 = (I1[])obj; // 4 Here at runtime obj refers to base object and is assigned to object of type I1 which is parent of base. Definitely this is valid at runtime.

Hope this clears



I think i1 is not parent of base. I1 is an interface reference to an object of type base. I feel Line 4 is valid at runtime because an object reference (obj) can be casted to an interface reference (i1) if the object's class implements that interface. (class base implements I1 in this case)

Please correct me if I am wrong.

Thanks
Karthik
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic