• 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:

Casting

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys,
I have a question about casting in this code from one of the devaka K examlabs........

Kinda confused with the whole typecasting thing.......


class B extends A{}
final class C extends B{}

class A{
public static void main(String args[])
{
A a = new B();
B b = new B();
C c = new C();
C[] ca = null;
a = b;
b = c;
A a1 = (B) c;
Runnable r1 = (Runnable) a; //1
Runnable [] ra1 = null;
A r2 = (A) r1;
A[] aa1 = (A[])r1; //17
A[] aa2 = (A[])ra1; //18
ra1 = (Runnable[]) ca; //19
}
}



Why is line 19 giving compile time error when line 18 isn't?

Thanks in advance!!!
 
rhett howard
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even //1 doesn't compille when i say


Runnable r1 = (runnable) c;


Is it because class c is final???...


If that is so, I am even more confused???
 
rhett howard
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Okie, I understand it something like this......

Since C can't be subclassed anymore, compiler figures out there can't be any subclass which can implement Runnable and so it won't allow any casting to runnable???.......

Looks like thats the answer!!.....Let me know if it isn't......

Now i wonder why i even posted this question???......

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

rhett howard wrote:
Okie, I understand it something like this......

Since C can't be subclassed anymore, compiler figures out there can't be any subclass which can implement Runnable and so it won't allow any casting to runnable???.......


Yeah, that's it.

rhett howard wrote:
Now i wonder why i even posted this question???......


Let see it is as a therapy. Loud thinking. You listen to your echo and understand the problem. It works most of the time.

cheers
Bob
reply
    Bookmark Topic Watch Topic
  • New Topic