• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Inheritance Casting 1Z0-803

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI
Im hoping some of you very smart people here can explain what exactly is happening with the following code.
This question is for the OCA Java 7 exam. It is supposed to be simple enough but Im finding it difficult to understand
the sequence of the code. Here is the code simple enough

Interface I{}
class A implements I()
class B extends A{}
class C extends B{}

The following declarations:
A a = new A();//Easy enough set up in a test class
B b = new B();//Easy enough set up in a test class

...............................................................................................................................................................

which of the following will compile?

1/ a = (B)(I)b; // Is b cast to I then I cast to B??.....Any Help here please??

2/ B = (B)(I) a; // Is it trying first to cast a to an I ???

3/ A = (I) b;// ???

4/ I = (C) a;// ???
..............................................................................................................................................................................................
There are answers to this question but they do not help in my understanding of the question they are...

a = (B)(I)b;
class B does implement I because it extends A, which implements I. A reference of type I can be cast to any class at compile time.
Since B is-a A, it can be assigned to a.

B = (B)(I) a;
This will fail at run time because a does not point to an object of class B.

A = (I) b;
An I is not an A. Therefore, it will not compile.

I = (C) a;
It will compile because a C is-a A, which is-a I, and a reference of class A can point to an object of class C.
But it will fail at runtime because it does not point to an Object of class C


Any further explanations much appreciated Thanks!


 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Where this question comes from?
Please, QuoteYourSources (this is a link).
 
David Pemberton
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:Welcome to the Ranch!

Where this question comes from?
Please, QuoteYourSources (this is a link).




Apologies this is a question from Enthuware Test Studio for OCA 7/ 1z0-803 Thanks
 
author
Posts: 23928
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

David Pemberton wrote:
1/ a = (B)(I)b; // Is b cast to I then I cast to B??.....Any Help here please??

2/ B = (B)(I) a; // Is it trying first to cast a to an I ???



Yes. Both examples require that the instance be IS-A two types, as it is cast twice.

Henry
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure about options 3 and 4? The syntax doesn't make sense.
 
Henry Wong
author
Posts: 23928
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

Keith Lynn wrote:Are you sure about options 3 and 4? The syntax doesn't make sense.



Yeah, option two also has a syntax error... all of which I attributed to a copy error when the OP retyped the question (based on the explanation).

Henry
 
David Pemberton
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Keith Lynn wrote:Are you sure about options 3 and 4? The syntax doesn't make sense.



Yeah, option two also has a syntax error... all of which I attributed to a copy error when the OP retyped the question (based on the explanation).

Henry



Yes well spotted my silly s*u*id mistake.............. should be

a = (B)(I)b;

b = (B)(I) a;

a = (I) b;

I i = (C) a;

Apologies and thanks for your patience!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David Pemberton,

First of all, a warm welcome to CodeRanch!

David Pemberton wrote:Any further explanations much appreciated Thanks!


This exact same question is explained in great detail in this thread. So take your time to carefully read the complete thread. And if you stil have questions/doubts, just let us know by hitting the Post Reply button

Hope it helps!
Kind regards,
Roel
 
David Pemberton
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Hi David Pemberton,

First of all, a warm welcome to CodeRanch!

David Pemberton wrote:Any further explanations much appreciated Thanks!


This exact same question is explained in great detail in this thread. So take your time to carefully read the complete thread. And if you stil have questions/doubts, just let us know by hitting the Post Reply button

Hope it helps!
Kind regards,
Roel



Thanks Roel and all who replied fantastic help thanks!!!
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic