About Question enthuware.ocajp.i.v7.2.1372 :
Moderator: admin
-
- Posts: 20
- Joined: Sun Feb 16, 2014 3:28 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1372 :
How come option 3 throws ClassCastException
Though String class and Object class are related
Though String class and Object class are related
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
I am not sure what is your current understanding of when CCE is thrown. Can you post it here so that I can address your doubt?
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Sun Feb 16, 2014 3:28 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
Well to me,a CCE occurs when an Object fails an IS-A test with the class type into which it is being Casted(Book-Mala Gupta).
And every class extends Object class.
And every class extends Object class.
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
So is that happening or not happening here?
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Wed Nov 05, 2014 8:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
I need a bit of clarification on why casting and object to String throws ClassCastException.
But I'll state what I think first, then ask my questions.
Here's example code in the Mala Gupta book.
So line that throws the exceptions throws it because it is trying to cast a ColorInk object as a BlackInk object but BlackInk isn't a ColorInk. I assume the code would work if I were to try and cast to Ink? Since ColorInk is an Ink.
So in the case of the mock exam question, why does casting an Object to String fail, isn't String an object and thus implicitly extends Object?
What am I missing?
But I'll state what I think first, then ask my questions.
Here's example code in the Mala Gupta book.
Code: Select all
import java.util.ArrayList;
public class ListAccess {
public static void main(String[] args) {
ArrayList<Ink> inks = new ArrayList<Ink>();
inks.add(new ColorInk());
inks.add(new BlackInk());
Ink ink = (BlackInk) inks.get(0); // Throws ClassCastException
}
}
class Ink{}
class ColorInk extends Ink{}
class BlackInk extends Ink{}
So in the case of the mock exam question, why does casting an Object to String fail, isn't String an object and thus implicitly extends Object?
What am I missing?
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
You said casting ColorInk to Ink should work because ColorInk extends Ink.
If you apply the same logic in this question, why do you think casting Object to String should work? Dpes Object extend String?
If you apply the same logic in this question, why do you think casting Object to String should work? Dpes Object extend String?
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.v7.2.1372 :
The compiler says: java.lang.Object cannot be cast to java.lang.String
But why? I thought String extends Obects.
But why? I thought String extends Obects.
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
Think of it this way - Dog extends Animal, What does that mean? Every Animal is a Dog? or Every Dog is an Animal?
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Fri Sep 29, 2017 3:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
String extends Object therefore Object pointer can point to a String object.
String s ="test"
Object o = s;
Now you can do all methods of Object class for that string.
Every String is an Object.
Also methods with Object in their argument accepts String as a parameter.
The other direction is not like that. Not every object is a String.
Therefore cast exception raid for:
Object o = new Object();
String s = o // compile error.
String s = o.toString(); // ok!
String s ="test"
Object o = s;
Now you can do all methods of Object class for that string.
Every String is an Object.
Also methods with Object in their argument accepts String as a parameter.
The other direction is not like that. Not every object is a String.
Therefore cast exception raid for:
Object o = new Object();
String s = o // compile error.
String s = o.toString(); // ok!
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
I don't understand the issue in the first code snippet?
To me the code looks fine, and it has the boundary condition .
To me the code looks fine, and it has the boundary condition .
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
The first one uses recursion. It will cause the stack to overflow if you run it for a large integer. For example, try calling it - factorial(Integer.MAX_VALUE)
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Mon Jul 09, 2018 4:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
The questions asks about exceptions, but the right answer has an error in it? Those are different branches of the Throwable tree. Or do I see this the wrong as a non-native English speaker?
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
The word "exception" (with a lower case e) implies all sorts of exceptions i.e. all instances of Throwable. The word "Exception" (with an upper case E) implies only Exception and its subclasses (including RuntimeExceptions). This is common terminology and is used by the exam as well.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Sat Sep 04, 2021 8:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
I don't understand the third one. I thought it was "No exception will be thrown".
Why it caused ClassCastException ?
Also where can I find the "ExceptionClassSummary document in the "Study References" section."?
Why it caused ClassCastException ?
Also where can I find the "ExceptionClassSummary document in the "Study References" section."?
-
- Site Admin
- Posts: 10064
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1372 :
The call to m1() returns an object of class java.lang.Object. Since Object does not satisfy the is-a relation with String (because an Object is not a String, although a String is an Object), if you try to cast it to String, the JVM will throw a ClassCastException.
It is like trying to force a Vehicle to be a Car. All Vehicles are not Cars even though all Cars are Vehicles.
You can read Exception class summary here.
It is like trying to force a Vehicle to be a Car. All Vehicles are not Cars even though all Cars are Vehicles.
You can read Exception class summary here.
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 101 guests