About Question enthuware.ocajp.i.v8.2.1250 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
rp.reshma
Posts: 3
Joined: Wed Feb 15, 2017 8:55 am
Contact:

About Question enthuware.ocajp.i.v8.2.1250 :

Post by rp.reshma »

class B {}
class B1 extends B {}
class B2 extends B {}
public class ExtendsTest{
public static void main(String args[]){
B b = new B();
B1 b1 = new B1();
B2 b2 = new B2();
// insert statement here
}
}

why this b1 = (B1) b; throw exception

rp.reshma
Posts: 3
Joined: Wed Feb 15, 2017 8:55 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1250 :

Post by rp.reshma »

please reply someone to help for this topic .I m preparing for IZ0-808

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1250 :

Post by admin »

Did you read the explanation that is already given with this option, "It will pass at compile time but fail at run time as the actual object referenced by b is not a B1."

You are telling the compiler that the object referred to by b will be of class B1 at runtime. The compiler accepts that. But at run time, b is pointing to an object of class B. B is not B1, so the JVM will throw a ClassCastException.
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1250 :

Post by Sergey »

Code: Select all

class B {}
class B1 extends B {}
class B2 extends B {}
public class ExtendsTest{
  public static void main(String args[]){
     B b = new B();
     B1 b1 = new B1();
     B2 b2 = new B2();
     // insert statement here
  }
}
By casting b to (B1), you are telling the compiler that the object referred to by b will be of class B1 at runtime. The compiler accepts that because it is possible for b to refer to an object of class B1 since B1 is-a B.
It`s ok, i understand it.
However, at run time, b is pointing to an object of class B. B is not B1, so the JVM will throw a ClassCastException.
Excuse me, but how could we know that b is pointing to an object of class B? Which line of code tells us about it?

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1250 :

Post by admin »

The line B b = new B(); makes it quite clear that b points to an object of class B.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1250 :

Post by OCAJO1 »

The explanation for the last option states that b1 = (B1) (B) b1; will compile and runs fine. It does. Although is taking the long way of assign b1 to itself!

But is there ever a need for double class (no interface involved) casting?

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1250 :

Post by admin »

No, there is never a need for this.
If you like our products and services, please help us by posting your review here.

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1250 :

Post by baichen7788 »

i've done many questions.
such casting questions always confuse me.
I wonder if there will be a lot of casting in real java development work.

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1250 :

Post by admin »

A well designed application code rarely requires casting. But understanding casting is important to be able to produce a good design.
Casting is not really that complicated but if you are finding it too confusing then it indicates that you have not mastered the basics of polymorphism and OOP. We would suggest you to go through a good book to learn the basics. We recommend OCP Java 11 Part 1 Fundamentals by Hanumant Deshmukh.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest