• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

OCAJP 7. Casting

 
Ranch Hand
Posts: 89
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a question on casting. The process of valid upcasting and downcasting is plain:



When two classes are not in the same inheritance branch, it is also quiet clear, the compiler will throw a compilation error. But what about interfaces? Why the code snippet below regarding 'Phone' instance and 'Roamable' interface compiles fine?

 
Ranch Hand
Posts: 472
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this will help



thats why reference type Phone may contain class iPhone that implement interface Roamable. and as you can store object iPhone in Phone you can cast it to interface Roamable.
thats behavior will nullify if you mark Phone as final.
 
Sergei Zhylinski
Ranch Hand
Posts: 89
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sergej Smoljanov wrote:this will help



Thanks. I was experimenting with String and StringBuffer and recollected, that they are both final. I managed to worked that out by myself.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sergey Zhylinsky wrote:But what about interfaces?


That can be summarized in a very simple rule: if your class ClassX isn't final, you can cast a reference variable of ClassX to any interface. If ClassX is final, casting to any interface not implemented by ClassX (or its parent classes) will result in a compiler error.

The reason behind this rule is also very easy and makes a lot of sense: if ClassX isn't final, ClassY can extend ClassX and implement InterfaceA which isn't implemented by ClassX. Therefore a reference variable of ClassX can refer to a ClassY instance, and then the cast to InterfaceA is valid (and no compiler error should be given). This scenario is also illustrated with a code example in the post of Sergej Smoljanov. If ClassX is final, the compiler knows this class can't be subclassed and thus the cast is invalid and the compiler gives one of his shiny errors
 
Hey! Wanna see my flashlight? It looks like 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