Page 1 of 1

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

Posted: Sun Feb 19, 2017 1:18 pm
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

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

Posted: Sun Feb 19, 2017 10:23 pm
by rp.reshma
please reply someone to help for this topic .I m preparing for IZ0-808

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

Posted: Sun Feb 19, 2017 10:34 pm
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.

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

Posted: Tue Aug 01, 2017 4:01 pm
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?

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

Posted: Tue Aug 01, 2017 6:20 pm
by admin
The line B b = new B(); makes it quite clear that b points to an object of class B.

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

Posted: Wed Dec 05, 2018 7:53 pm
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?

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

Posted: Wed Dec 05, 2018 10:21 pm
by admin
No, there is never a need for this.

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

Posted: Sun Jun 06, 2021 5:36 am
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.

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

Posted: Sun Jun 06, 2021 6:08 am
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.