• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Object Assignment help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one explain how this compiles:
COnsider the following classes/interfaces
interface Intf10{}
class Base1010 implements Intf10{}

Base1010 b10 = new Base1010();
Runnable r2 =(Runnable)b10; <--- How does this compile

Also, this
Object 0 = new Object()
Runnable r3 =(Runnable)0;
My understanding is that since both Base1010 abd Object do not implement the Runnable interface at all, they should not compile.
But it does.It was a different question if the interface handle
was Intf10.
I know that there will definitely be a runtime exception. But why no compile time ?
Any explaination.
Thanks.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can refer Java Language Specification
Second Edition
5.5

If S is a class type:
If T is a class type, then S and T must be related classes-that is, S and T must be the same class, or S a subclass of T, or T a subclass of S; otherwise a compile-time error occurs.
If T is an interface type:
If S is not a final class (��8.1.1), then the cast is always correct at compile time (because even if S does not implement T, a subclass of S might).
If S is a final class (��8.1.1), then S must implement T, or a compile-time error occurs.
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sandy,
It is weird, but this is one of those quirks you have to remember. According to Bill Brogden Exam Cram book:


    A class reference can be converted to:
  • any super class type, including Object
  • any class reference with runtime check
  • any interface reference if the class implements the interface
  • any interface reference with runtime check


You're thinking of the 3rd bullet. In this case, it's the last bullet that is applicable and the reason the program is able to compile. However, at runtime you'll get a ClassCastException.
-Peter
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandy,
A good way to think about it is this:
If you use casting the compiler doesn't check it!
By using casting you are telling the compiler I know more than you do about the variable, so just shut up and do what I tell you!
If you had not used casting the compiler would then make sure that the class implements the interface; hence giving you a compiler error.
Peter is right, we have fooled the compiler, but at runtime the JVM will not be so easily fooled: ClassCastException.
Regards,
Manfred.
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi peter,
Can please you explain further "interface reference " - Can we have an interface reference ? that is instantiating reference ? I am trying to understand the terminilogy:
Also when you said " runtime check " - you mean using instanceof operator Right?
Thanks
Shrinivas
 
Sandy Lee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manfred,
I would say, that is not entirely true.
Try casting say ClassA to say another class say ClassA where in both are separate classes defined by the programmer.
It will not compile.
Thanks all of you for your answers.
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shrini,
A reference refers to the variable type. In this case, the variable type is an interface. That's why it's referred to as an interface reference. E.g.

A class reference is when you declare the type is a class. E.g.

Runtime check is check that is not done by the compiler. It's not using the instanceof. It's done by the JVM at runtime. When the JVM gets to the line Runnable r3 = (Runnable)b10; it checks to see if b10 (or any superclass of b10) implements Runnable. If it doesn't, then a ClassCastException is thrown. If the code never gets to this line, then no "runtime" check is ever done. Runtime check (if there are any) only happens for code that gets executed by the JVM.
-Peter
 
Shrini Kulkarni
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Peter !! I got the point.
Shrinivas
 
I'm doing laundry! Look how clean this tiny ad is:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic