Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Sun Mar 30, 2014 4:46 am
by arjun gaur
How come option 3 throws ClassCastException
Though String class and Object class are related

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Sun Mar 30, 2014 5:36 am
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Tue Apr 01, 2014 2:30 am
by arjun gaur
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.

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Tue Apr 01, 2014 3:48 am
by admin
So is that happening or not happening here?

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Sat Nov 15, 2014 5:46 am
by bogieman987
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.

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 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?

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Sat Nov 15, 2014 6:03 am
by admin
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?

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Tue Sep 26, 2017 3:27 pm
by Sergey
The compiler says: java.lang.Object cannot be cast to java.lang.String
But why? I thought String extends Obects.

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Tue Sep 26, 2017 9:56 pm
by admin
Think of it this way - Dog extends Animal, What does that mean? Every Animal is a Dog? or Every Dog is an Animal?

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Sun Oct 08, 2017 4:21 am
by zoharch
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!

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Sun Jun 17, 2018 1:26 pm
by flex567
I don't understand the issue in the first code snippet?
To me the code looks fine, and it has the boundary condition .

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Mon Jun 18, 2018 2:31 am
by admin
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)

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Mon Jul 09, 2018 4:50 am
by roberto1
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?

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Mon Jul 09, 2018 5:21 am
by admin
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.

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Tue Sep 14, 2021 11:39 pm
by qazwsx922
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."?

Re: About Question enthuware.ocajp.i.v7.2.1372 :

Posted: Wed Sep 15, 2021 12:01 pm
by admin
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.