Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance Casting 1Z0-803

 
David Pemberton
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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!


 
Paweł Baczyński
Bartender
Posts: 1878
35
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure about options 3 and 4? The syntax doesn't make sense.
 
Henry Wong
author
Marshal
Pie
Posts: 22119
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • 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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic