This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please HELP...Devaka Exam Test 2 Number 69. I totally don't understand it. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please HELP...Devaka Exam Test 2 Number 69. I totally don Watch "Please HELP...Devaka Exam Test 2 Number 69. I totally don New topic
Author

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

Eko Harmawan
Greenhorn

Joined: Apr 17, 2009
Posts: 12
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

Joined: Mar 15, 2009
Posts: 452

//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


SCJP 5.0 SCWCD 5.0
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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

Joined: Apr 24, 2009
Posts: 30
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

Joined: Mar 04, 2009
Posts: 417
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

Joined: Apr 17, 2009
Posts: 12
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

Joined: Mar 04, 2009
Posts: 417
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

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


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

Someone just confirm that this question is out of scope of SCJP.


===Vyas Sanzgiri===
My Blog
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3013
    
  35

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.


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
Eko Harmawan
Greenhorn

Joined: Apr 17, 2009
Posts: 12
Thank you every one. I get it know.
 
jQuery in Action, 2nd edition
 
subject: Please HELP...Devaka Exam Test 2 Number 69. I totally don't understand it.
 
Similar Threads
Inheritance and Polymorphism Involving Interface Casting
Casting
Array Assignments
Doubt about casting and final classes
Bugtraking Devaka's Examlab