Win a copy of Spark in Action this week in the Open Source Projects 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Understanding Inheritance Flash cards

 
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got following question on Kalpan Self Test for 1Z0-803 Can anyone help me out for the same??


Which of the following statements are true I ticked wrong answer and correct answer was following please look into image for more detais

Reference type casting :the target type must be object type or a valid supertype

Thanks in advance.
Kalpan-Test1.png
[Thumbnail for Kalpan-Test1.png]
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's look at an example. Suppose we have this class hierarchy:
-- Object
---- Person
-------Yogesh

Now we have some code:



The cast to Object is redundant but it works. The cast to Yogesh is a problem because Yogesh is a subclass of Person. By contrast, the following case is fine.



So we can see from this that the actual object type must be a match or a supertype of the what we are casting to. In the first example, Person has Object as a supertype so is good but Yogesh as a subclass so not good.
 
You didn't tell me he was so big. Unlike 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
    Bookmark Topic Watch Topic
  • New Topic