• 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

Class Casting

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



My doubt is:

Class C is implementing the serializable interface.
Then why cant the anonymous inner class new Serializable(){}; be cast into C?

I am very confused with class casting and which casting are appropriate and which are not..
Any pointers to resources explaining this very well? Thanks a lot in advance.


[Edit] I am unable to upload images. How can i do that? I gave the full url (including the filename) in the Img tag.
is there some format limitation?? i tried with .jpg and .bmp
 
author
Posts: 23951
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

Rikesh Desai wrote:
[Edit] I am unable to upload images. How can i do that? I gave the full url (including the filename) in the Img tag.
is there some format limitation?? i tried with .jpg and .bmp



The image tag is not for uploading images. It is for the specification of images already reachable via the internet. To upload an image, choose the attachment tab on the reply/edit page (below the text screen). And specify the images to upload from there.

Henry
 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh ok.

Have attached the screenshot of my doubt.
doubt.jpg
[Thumbnail for doubt.jpg]
Doubt
 
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Rikesh ..

you can downcast in same hierarchy no matter if it is legal or not ..compiler won't show any error regarding that it will show runtime error for illegal downcasting..

and here ....

in line 4
c=(C) new Serializable();

here you are trying to instantiate Serializable interface and which is not legal so it is showing compiler error..


but i am also confused here in line 2
d=(D)ob;
here i am also confused because both are of different hierarchies then also it is not showing any compiler error.....

someone more knowledgeable please help regarding this..
 
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

ankur trapasiya wrote:

in line 4
c=(C) new Serializable();

here you are trying to instantiate Serializable interface and which is not legal so it is showing compiler error..


but i am also confused here in line 2
d=(D)ob;



The image is not much visible to me but I don't think that line is c = (C) new Serializable()
I think that is
c = (C)new Serializable(0{}; // this is different from above one, this is inner anonymous class
// and you will study this in the 8th chapter

now the second question is about casting the Object into D

this is valid as at compile time we can cast to anything across the same hierarchy,

and D extends Objects
so this is possible

 
Rikesh Desai
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes Prasad,

it is an anonymous inner class... then why is it still invalid? though class C is implementing the serializable interface?

and writing an anonymous class for an interface means that we are implementing that interface right??
then why still is it giving compiler error?
 
ankur trapasiya
Ranch Hand
Posts: 160
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
prasad and rikesh sorry i didn't see last curly braces...

but still i am thinking that you cannot make anonymous class of marker interfaces.....because it does not going to have any method so they will be treated as interfaces by compiler..
 
Prasad Kharkar
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
@ankur
this is not like that
by creating anonymous class consider following

  • when we are creating anonymous class, that either implements an interface or extends the class
  • you say we cannot do that as we have no methods to implement
  • but we can createin instance of class without any methods isn't it?




  • happy preparation
     
    ankur trapasiya
    Ranch Hand
    Posts: 160
    Netbeans IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hmmm right ..

    well i tested this and as predicted it works fine

    Serializable s=new Serializable(){public void display(){}};

    but here what it is trying to do is casting Anonymous class which is Serializable to the class type of C which is in a complete different class hierarchy..........

    so definately it is not going to allow us to compile this code....
    Filename: Rickesh.bmp
    Description: What we are trying to do
    File size: 590 Kbytes
     
    Rikesh Desai
    Ranch Hand
    Posts: 83
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    got the point.
    thanks guys for all the help!
     
    ankur trapasiya
    Ranch Hand
    Posts: 160
    Netbeans IDE Java Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    from my side thanks to you for posting such a good mind bending question.....even i also didn't know this before testing this program... .
     
    Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic