• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Anonymous class..

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi , i got it from Devaka's lab

is it true that Anonymous class cannot be used to cast?

class A{}
class B exetnds A implements java.io.Serializable{}
class casting
{
public static void main(String args[])
{

A a=new A();
B b=new B();
b=(B)new Serializable(){};//compiler error and says that inconvertible types

}
}


Thanks
Preetha
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as understood ,anonymous class EXTENDS a class.
b=(B)new Serializable(){};

So here you are trying to extend a interface(Serializable).

Somebody correct me if i am wrong.
 
Ranch Hand
Posts: 171
Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
new Serializable(){} is implementing the inteface Serializable to create an anonymous class object.
In case of explicit casting,at compile time, when both the source type and target type are classes,one class must be a subclass of the other and new Serializable(){} is not a subclass or superclass of B.
[ December 27, 2008: Message edited by: Daisy Lakhanpal ]
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Tharakan:
As far as understood ,anonymous class EXTENDS a class.
b=(B)new Serializable(){};

So here you are trying to extend a interface(Serializable).

Somebody correct me if i am wrong.



Sorry James, you are wrong. Anonymous inner classes can be used to both 'extends' and 'implements' tasks.

The problem is with the "reference type" of an anonymous inner class. Look at that anonymous inner class declaration fragment:
new Serializable(){}

How do you think about the above inner class expression? Do you think it is same as to, something like:
Serializable innerClass = new Anonymouse();

No! But actually it is same as to:
Anonymous innerClass = new Anonymouse();

(where Anonymous is something that implements the Serializable interface)


Now consider the following program:




Both of the above two statements gives you a compile time error, because of the SAME reason. Try to understand through the above program.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the correction...
Now i realize how stupid i was when i posted it.
Now things are clear
[ December 25, 2008: Message edited by: James Tharakan ]
 
CAUTION! Do not touch the blades on your neck propeller while they are active. Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic