This week's giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of liveProject: Build an ML Recommender System and have Kim Falk on-line!
See this thread for details.
Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Casting to interface

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why compiler allows cast classes for any interface?
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

So that Polymorphism comes into play !!!

Best of luck ...
 
Michal Adamski
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23899
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

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
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


No. Maybe you did not notice the curly brackets. The interface is being implemented.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic