• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Doubt about casting and final classes

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
this code is taken from exam 2 of examlab:



I don't understand why the marked lines give error.

Can someone explain to me ?

Thanks.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It's quite simple. Those lines are reporting an error because the compiler, can determine at compile time, that those assignments are not possible. I think it is more interesting to note why some of the other lines *are* possible.

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

Henry Wong wrote:

It's quite simple. Those lines are reporting an error because the compiler, can determine at compile time, that those assignments are not possible...

Henry



Yeah...I understood that by myself. My question is:
Why the compiler can determine that those assignments aren't possible ?

 
Henry Wong
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emanuele Ghe wrote:
Why the compiler can determine that those assignments aren't possible ?



Line 15 is pretty simple. Think about it. It is possible for any array class to implement the runnable interface? And if so... how?

Line 17 is a bit more complicated. Hint. Class C is a final class.

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

Henry Wong wrote:
Line 15 is pretty simple. Think about it. It is possible for any array class to implement the runnable interface? And if so... how?



Got it, thanks.

Henry Wong wrote:
Line 17 is a bit more complicated. Hint. Class C is a final class.



Still I am not sure on why/how the compiler is sure that assignment is wrong.

I'll try to explain what I think, please confirm me:
maybe, since class C is final, the compiler knows that the run-time object assigned to the 'c' reference variable can't be a subclass of C implementing runnable ?

But, if we anlyze this assignment:


Then the compiler can't know for sure that, at run-time, ra1 could be an array object of a class X such that


Am I right ?

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

Emanuele Ghe wrote:

Henry Wong wrote:
Line 15 is pretty simple. Think about it. It is possible for any array class to implement the runnable interface? And if so... how?



Got it, thanks.

Henry Wong wrote:
Line 17 is a bit more complicated. Hint. Class C is a final class.



Still I am not sure on why/how the compiler is sure that assignment is wrong.

I'll try to explain what I think, please confirm me:
maybe, since class C is final, the compiler knows that the run-time object assigned to the 'c' reference variable can't be a subclass of C implementing runnable ?

But, if we anlyze this assignment:


Then the compiler can't know for sure that, at run-time, ra1 could be an array object of a class X such that


Am I right ?



Right on. Since 'ca' is a reference variable to a final class, and the target to cast is an interface, it follows that in order to avoid the compiler error class C must implement Runnable. Actually there are a couple more rules involved here under the hood, you can check them here: Java Language Specification (links directly to casting conversion).
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
read the last post of william in this thread

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

Seetharaman Venkatasamy wrote:read the last post of william in this thread

hth



Thanks a lot, is very usefull.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Emanuele Ghe wrote:Thanks a lot, is very usefull.



you are welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic