1

I was asked in an interview to give an example of a ClassCastException for which I gave the below example

class X{}
class Y extends X{}
class Z extends X{}
Y y=new Y();
Z z=new z();

(X)y is possible (X)z is possible

Z(y) and (Y)z will throw a ClassCastException. Then the interview asked me to give real world example for which I said both boat and table are made up of wood

table extends wood boat extends wood but you can not use table for fishing similarily you can not use table to sir or stand

I want to know if my examples are right or not?

JimmyB
  • 12,101
  • 2
  • 28
  • 44
rocking
  • 4,729
  • 9
  • 30
  • 45
  • `(Z)y` and your other example would not compile. `Z` and `Y` are not in the same inheritance tree. Also, it's trivial to try and compile that code and run it to see what happens. Just do it. `X x = new Y();` and `(Z)x` would cause a `ClassCastException`. – Sotirios Delimanolis Jul 08 '15 at 14:51
  • @downvoter if you dont like my question simply write a comment,I will delete my post staright away – rocking Jul 08 '15 at 14:51
  • Possible duplicate of http://stackoverflow.com/questions/907360/can-someone-explain-classcastexception-in-java – Mudassar Jul 08 '15 at 14:55
  • As @SotiriosDelimanolis pointed out your example is difficult to use to create a compiling example which will fail with a ClassCastException at runtime. It's much more common to run across things like `X y = new Y(); X z = new Z(); (Z)y;` – azurefrog Jul 08 '15 at 14:57
  • Typically, the example is given with `Animal` and specializations thereof. A `Dog` is an `Animal`, so is a `Cat`. An `Animal` can walk, so `Cat` and `Dog` can walk. Walking a `Dog` can be done the same way as walking a `Cat`, or any `Animal`, so you can pretend that there's no difference between `Cat` and `Dog` when walking. But you cannot just pretend a `Cat` is a `Dog` to make the `Cat` bark. – JimmyB Jul 08 '15 at 15:00
  • @SotiriosDelimanolis yes true, its not compiling. Do you think the real time example is also wrong? – rocking Jul 08 '15 at 15:03
  • @HannoBinder good example,what about my example,is it wrong? – rocking Jul 08 '15 at 15:04
  • I, too, think the table/boat/wood example is not a good one. Inheritance is also referred to as "is-a" relationship, or "specialization". "Table *is a* wood" doesn't work out, "table *is a special kind of* wood" is not much better, except maybe if you're a firefighter or pyromaniac; then you may only be interested in the ability of an object to burn, irrespective of its shape or other functions. – JimmyB Jul 08 '15 at 15:06

2 Answers2

1

I would not have accepted that as an answer because a boat/table is not a subtype of wood.
A subtype of wood would be Oak or Cherry.

JimmyB
  • 12,101
  • 2
  • 28
  • 44
Pim Verkerk
  • 1,066
  • 7
  • 12
  • He did get the concept though. – Bauss Jul 08 '15 at 15:05
  • boat/table is not subtype but indirectly extends wood like Integer extends Object – rocking Jul 08 '15 at 15:08
  • @Bauss, I agree. This was not a conceptual error, rather a technical inaccuracy. – Pim Verkerk Jul 08 '15 at 15:09
  • Sorry, but I disagree. There are many possible relationships between the artifacts in a program, and upon implementing one has to choose which relationship is to be used where. Of course, there is *some* relationship between wood and table, but in most (real-world) cases this is not manifested as a specialization. It's probably another kind of *association*. Hence, as the interviewer, I wouldn't be sure if the answer indicates just a technical inaccuracy or a real misconception. – JimmyB Jul 08 '15 at 15:13
  • @rocking, A boat could be created without using wood. An Integer can't be created without being an Object. – Pim Verkerk Jul 08 '15 at 15:17
  • @PimVerkerk I am talking about wooden boat – rocking Jul 08 '15 at 15:18
  • I am aware the boat in your example is made out of wood. But to say a boat is a subtype of wood means all other boats must we wood too. – Pim Verkerk Jul 08 '15 at 15:28
1

z = (Z)y will not throw a ClassCastException - it will never compile.

The following will compile and throw a ClassCastException (java.lang.ClassCastException: [LXXX$Y; cannot be cast to [LXXX$Z;) on the second line

    X[] xx = new Y[1];
    Z[] zz = (Z[]) xx;

In the same spirit

   Object obj  = new Integer(0);
   String str = (String) obj; // class cast exception here
David Soroko
  • 8,521
  • 2
  • 39
  • 51