About Question enthuware.ocajp.i.v8.2.1250 :
Moderator: admin
-
- Posts: 3
- Joined: Wed Feb 15, 2017 8:55 am
- Contact:
About Question enthuware.ocajp.i.v8.2.1250 :
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
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
-
- Posts: 3
- Joined: Wed Feb 15, 2017 8:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1250 :
please reply someone to help for this topic .I m preparing for IZ0-808
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1250 :
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.
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.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1250 :
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
}
}
It`s ok, i understand it.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.
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?However, at run time, b is pointing to an object of class B. B is not B1, so the JVM will throw a ClassCastException.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1250 :
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.
-
- Posts: 221
- Joined: Mon Nov 26, 2018 2:43 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1250 :
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?
But is there ever a need for double class (no interface involved) casting?
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1250 :
No, there is never a need for this.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Fri Mar 26, 2021 7:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1250 :
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.
such casting questions always confuse me.
I wonder if there will be a lot of casting in real java development work.
-
- Site Admin
- Posts: 10068
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.1250 :
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.
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.
Who is online
Users browsing this forum: No registered users and 153 guests