aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Casting to interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Casting to interface" Watch "Casting to interface" New topic
Author

Casting to interface

Michal Adamski
Greenhorn

Joined: May 30, 2007
Posts: 5
Why compiler allows cast classes for any interface?
Vassili Vladimir
Ranch Hand

Joined: Mar 08, 2007
Posts: 1585
Hi,

So that Polymorphism comes into play !!!

Best of luck ...


Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Michal Adamski
Greenhorn

Joined: May 30, 2007
Posts: 5
class A {};

class Test {

A a = (A)(Appendable)this; // OK (or any interface insted of Appendable)
A a = (A)this // Compilation fails
A a = (A)(String)this // Compilation fails
}

Could you explain in more details why is it like this?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18120
    
  39

Originally posted by Michal Adamski:
Why compiler allows cast classes for any interface?


The compiler will allow a cast to an interface, when it can't be sure if the object implements that interface.

Try casting a String object to the Map interface. It will not work -- the compiler will complain. The reason is because the String class is final, it doesn't implement the Map interface, nor can it be subclassed to create a String object that implements that interface.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Notice also that in the class Test above (which needs a little help to run), the declaration: A a = (A)(Appendable)this; will compile but fails at runtime with a ClassCastException.

I have just realized that we are not casting to an interface but from an interface to the class A.
[ May 30, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

The following link explains why object-reference variables can and should be declared as variables, parameters, and return types.

Programming to an interface.

Kaydell

[ Edited to be more on-topic. ]
[ June 01, 2007: Message edited by: Kaydell Leavitt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Referring to the topic in question, this example:


gives an idea of what you can do. Notice that the cast to A compiles but thing is not actually referring to an A, so it fails at runtime. In the case of the cast to B, thing1 actually refers to a B so it does not give an exception.
Neha Monga
Ranch Hand

Joined: Mar 13, 2007
Posts: 34
Hello Barry!

In your above code snippet, interface I is being instantiated. As far as I know, interfaces cannot be instantiated. Am I missing something? Pls correct me if I am wrong.

Thanks!
Neha Monga
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

In your above code snippet, interface I is being instantiated.

No. Maybe you did not notice the curly brackets. The interface is being implemented.


[My Blog]
All roads lead to JavaRanch
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Hi Satou,

Yes. Thats right. Even just now i also noticed. But i remember the code was like just instantiated.

Thanks for that.
[ May 30, 2007: Message edited by: Raghavan Muthu ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Casting to interface
 
Similar Threads
Time Capsule
Regarding Interfaces
How To Call Two Different Classes From the Main Program Using the Same API?
Home interface or Home Interface stub
Writing an Interface