• 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

Please HELP...Devaka Exam Test 2 Number 69. I totally don't understand it.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I took Devaka exam test 2, in question 69 drag and drop which asked me to decide which code compiles and which is not. This is the code


It confuses me why there's a cast that i think it should be compile error but it doesn't. For example why the statement
Runnable r1=(Runnable)a;
is not giving compiler error. I think it should because A is not Runnable then the compiler should compiler for that incompatibility.


The second question, is based from that code that has been modified a little. this is the code

The class C is not declared as a final class. and the code
ra1=(Runnable[]) ca;
is not giving compiler error.

Please explain to me why this two happen. And if it mentioned in K&B book, please mention to me, I might miss it. Thank you very much
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

//Why this 3 line not error???
Runnable r1=(Runnable)a;
Runnable[] ra1=null;
A x=(A)r1;



in the first line you are telling that i know "a" is runnable so please let me do what i want.
compiler sees the possiblity while casting & in case of polymorphism it allows you to do this.

another example let



what will be the result? it will certainly give compile time error as the compiler knows that a1 & b1 are not in the same object graph. so it will give



but in the above case be sure that at runtime it will certainly fail with ClassCastException.

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

Eko Harmawan wrote:
The class C is not declared as a final class. and the code
ra1=(Runnable[]) ca;
is not giving compiler error.


I think there is something related with anonymous class, because, as you said, when C is not declared as a final class, there's no error.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
It is always legal to cast to an Interface - ALWAYS! (you need to check the SUN's specifications on that) no matter what the real object is or the interface is, UNLESS the class is declared to be final. There is a post on javaranch where this is explained... if only I could find it...
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eugene Rabii wrote:Hello,
It is always legal to cast to an Interface - ALWAYS! (you need to check the SUN's specifications on that) no matter what the real object is or the interface is, UNLESS the class is declared to be final. There is a post on javaranch where this is explained... if only I could find it...


Yes!
Just add that if the class doens't implement the interface, you will get a runtime exception (ClassCastException).
eg:

Exception in thread "main" java.lang.ClassCastException: Class1 cannot be cast to Interface1
 
Eko Harmawan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your for prompt response, but there's some concept that i need to make sure.

1)In Line 21 the statement
A[] aa2=(A[])ra1;
is not givin error. That statement attempt to cast Array to other type of array. But the ra1 is an array of runnable.
and in line 21 it tryin to cast an array to other type of array which is not in the same hierarchy type. So, the it a cast to a class type not an interface(array tipe which is object).

2)It still not makin any sense to me. In line 24, the statement doing a cast to array type not to interface type, isn't it?
It should be an error, at least that what i think right now.

Please help me to understand this really new stuff for me.
 
Leandro Coutinho
Ranch Hand
Posts: 430
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends you can find when compile time error occur and when runtime exception occur with casting.
You can go through this URL Java Language Specification 3.0 - Chapter 5.5 Casting Conversion
 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Someone just confirm that this question is out of scope of SCJP.
 
Sheriff
Posts: 7134
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vyas Sanzgiri wrote:Someone just confirm that this question is out of scope of SCJP.


Well, it is correct to say that this question is really tough than the level of the SCJP exam. However, this is not in out of scope of SCJP objectives anyway.
 
Eko Harmawan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you every one. I get it know.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic