This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Eko Harmawan
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Eugene Rabii
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • 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: 423
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Hibernate Linux Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Chrome Netbeans IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone just confirm that this question is out of scope of SCJP.
 
Devaka Cooray
ExamLab Creator
Marshal
Pie
Posts: 4116
195
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you every one. I get it know.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic