File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner Class Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Class Doubt" Watch "Inner Class Doubt" New topic
Author

Inner Class Doubt

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Source: Devaka Cooray's Exam Simulator




I am not able to understand why Line-4 is generating a compiler error?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Abhi, when you create the anonymous class instance it can only pass the Is-A test for Serializable, so the compiler will catch that it can never be cast to C. You could do it the other way around though (I think, you can try this and see if it works)

Serializable s = (Serializable) new C(){};

In this case, the instance of the anonymous class would pass the Is-A test for C, which itself passes the Is-A test for Serializable, so the cast would both compile and run.
[ December 28, 2008: Message edited by: Ruben Soto ]

All code in my posts, unless a source is explicitly mentioned, is my own.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
This happends as Anonymous class has no syntax to say it extends C implements Serializable.


class C_serializable extends C implements Serializable{
}


Above case is not possible in case of anonymous class, compiler can see this, will find that in

c=(C)new Serializable(){}; //Line-4


this scenario there is no way that this innerclass could be a child of class C.


SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Class Doubt
 
Similar Threads
Devaka's Practice Exam1
casting
Class Casting
Interface instance?
Casting a supertype to a subtype