• 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

Question on Casting and Exception

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

Which of the following statements can be inserted at "//Insert Here" to compile and run without any errors or exceptions?

Please explain why choice A gives a runtime error and not choice B when inserted independently at //Insert Here?



A. Runnable r1=(Runnable)c;
B. Runnable r2=(Runnable)d;

I do understand the reason for A... c doesn't implement Runnable. Not sure about B.

Thank you all.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If you see class D is declared final, so you can't change it to refer to something else.

If you see, final classes can be instantiated, but you can't change it's type, sub-class it or make the object reference to refer to something else.

Hope this helps,
 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prithvi,

Thanks for the reply. But if it is about final class, why would b=(B)d; work when inserted at //Insert Here?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tayitu,

Runnable r2=(Runnable)d; gives me a compile error also.

Where is this coding question coming from and are there really only two choices for answers.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Becase D is extending from B and both are in the same hierarchy so the cast is possible. If you see
Runnable doesn't come in the hierarchy of B or D. Even if you say something like this for example,


It will give you a compile time error, because G doesn't come in the inheritance hierarchy of B or D.

Even if you say


It will compile fine, because A is indirectly a super-class of D as B extends from A itself.

Hope this helps.

 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Larry,
Even if we say


It will work fine. Additionally, i just amended the code, so have a look at it.


It works fine.

HTH,
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Please quote the sources, from where you got this code snippet.

Best Regards,
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:Hello,

Please quote the sources, from where you got this code snippet.

Best Regards,


Thank you, Prithvi, I was questioning Tayitu's first posting and the two possible answer choices, A and B, that were provided.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

You are always welcome Larry. I always enjoy reading your threads.

Best Regards,
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:You are always welcome Larry. I always enjoy reading your threads.


I should mention there is another "Larry" in this forum. I am sure you are getting more enjoyment from the posts by "Larry Olson" from than mine.
 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys. Sorry for the confusion. The source is Exam Lab. Let me modify the code a little bit:-



My question is it is clear from the code that c doesn't implement Runnable... so why doesn't choice A give compile time error as choice B since both don't implement Runnable?
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Tayitu,

If you know Runnable is an interface. According to JLS if you see

Section 5.5 Casting

It says that if your class is a final class and you are trying to downcast it to an interface, then that class must implement
the interface else, the compile time error will occur. I will directly quote from JLS

If S is a final class, and T is an interface, then S must implement T, or a compile time error occurs.

So in your situation, you have a class marked final and you are trying to downcast it to an interface which is runnable
so a compile time error occurs. Try to omit final and try downcasting, it will work fine.

Even if B would have implemented the interface, it will let the casting happen.

Hope this helps,
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Larry Chung wrote:

Prithvi Sehgal wrote:You are always welcome Larry. I always enjoy reading your threads.


I should mention there is another "Larry" in this forum. I am sure you are getting more enjoyment from the posts by "Larry Olson" from than mine.



I meant Larry Chung. The motivator.

Best Regards,
 
Larry Chung
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for giving the reference to ExamLab.
Prithvi's explanations are right on target.

Another way to understand what happened is to think like the two players, the compiler and then the JVM.

When most Java compilers looks at Java code, they do not look or cannot look too deeply in the code. For this ExamLab code, the compiler simply sees that code snippet from B is wrong because class D is final and so quickly concludes there is no doubt that d cannot be cast as Runnable.

However, for code snippet from A, the compiler does not check if class C is Runnable by checking if parent class A is Runnable because class C did not implement Runnable. But what if the class hierarchy was much bigger? The compiler would take a long time to check all the parents. Therefore, it leaves that job to the JVM to report the issue at runtime.

When the JVM looks at object c, it sees all its inherited type information. The JVM determined that object c is only of type class C and inherited type class A. The JVM realize that it would be ridiculous to cast object c as Runnable because that object has no inherited Runnable feature.

Therefore, the result is:
code snippet A causes runtime error
code snippet B causes compile error
code snippet C causes no error
 
Tayitu Betule
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Prithvi and Larry for the detailed explanation and reference to JLS. That was really helpful.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome Tayitu.

Larry, that was an awesome explanation of thinking compiler and JVM as two players. Really enjoyed reading your explanation.

Warm Wishes,
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ha ha ha
this is my favorite
I was really confused about this kind of question some days ago
look
here c extends A but it does not implement the interface but there can be another class that extends C and implements the runnable interface isn't it, suppose class E extends C and implements Runnable then the compiler should allow the cast Runnable r1= (Runnable)c because there can be an E object that IS-A Runnable and C is the superclass of E hence the casting of C should be acceptable also but at this point of time the compiler does not check for the extended class
IN another case, about D we have declared it as final class and hence we cannot extend it
also we are not implementing the interface in it
at this point the compiler surely knows that the cast is not valid at all the times so it gives the compiler error
Hope this helps
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic