• 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

casting confusion

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


In the above code line 1 gives compiler error since we can’t cast B into A as they don’t belong to same inheritance tree.

But what about line 2?? should't it give a compiler error too? since A does not implement interface I.but it dosent and neither does it give a run time error.

what exactly are the rules for converting references of class and interface types?
[ October 06, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Change this line

A a = null;

to

A a=new A();


You will get run time error...

And...

One of the points in Narrowing conversion from the JLS says..


From any class type S to any interface type K, provided that S is not final and does not implement K. (An important special case is that there is a narrowing conversion from the class type Object to any interface type.)


So now if you change your class A as

final class A{void m(){}}

and then compile...You will get the compilation error...




Further reasoning would be appreciated...
[ October 06, 2005: Message edited by: A Kumar ]
 
anjali desh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx for the reply A.Kumar

So the logic is...
The compile time check is OK because of this condition "From any class type S to any interface type K, provided that S is not final and does not implement K"

And at run-time ,it is actually verified if object a which belongs to class A has implemented the interface I.and hence the class cast exception.
Am I on the right track?
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



And at run-time ,it is actually verified if object a which belongs to class A has implemented the interface I.and hence the class cast exception.




Yes I believe so...

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But actually, after i changed "A a = null;" to "A a = new A();", I still get the compile time error: can�t cast B into A.
 
anjali desh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We were referring to the compile time error due to line 2.The error due to line 1 will always be there.So comment it out and then see the output.
 
reply
    Bookmark Topic Watch Topic
  • New Topic