Lazy class cast in Java?

2013-03-13T23:46:57

Can someone please enlighten me as to why I don't get a ClassCastException in this snippet? I'm strictly interested into why it isn't working as I was expecting. I don't care at this point whether this is bad design or not.

public class Test {
  static class Parent {
    @Override
    public String toString() { return "parent"; }
  }

  static class ChildA extends Parent {
    @Override
    public String toString() { return "child A"; }
  }

  static class ChildB extends Parent {
    @Override
    public String toString() { return "child B"; }
  }

  public <C extends Parent> C get() {
    return (C) new ChildA();
  }

  public static void main(String[] args) {
    Test test = new Test();

    // should throw ClassCastException...
    System.out.println(test.<ChildB>get());

    // throws ClassCastException...
    System.out.println(test.<ChildB>get().toString());
  }
}

This is the java version, compilation, and run output:

$ java -version
java version "1.7.0_17"
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)
$ javac -Xlint:unchecked Test.java
Test.java:24: warning: [unchecked] unchecked cast
    return (C) new ChildA();
               ^
  required: C
  found:    ChildA
  where C is a type-variable:
    C extends Parent declared in method <C>get()
1 warning
$ java Test
child A
Exception in thread "main" java.lang.ClassCastException: Test$ChildA cannot be cast to Test$ChildB
  at Test.main(Test.java:30)

Copyright License:
Author:「Ionuț G. Stan」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/15389994/lazy-class-cast-in-java

About “Lazy class cast in Java?” questions

Can someone please enlighten me as to why I don't get a ClassCastException in this snippet? I'm strictly interested into why it isn't working as I was expecting. I don't care at this point whether ...
I am trying to write a unit test for my viewModel class which has lazy loading. I have following ViewModel class : abstract class DetailViewModel(item: TmdbItem) : BaseViewModel() { private val
I have a problem with casting a generic class to the interface it is implementing. My code is like this: interface foo { void foobar(); } class bar: foo { public void foobar() { ...
I'm aware that this could be a possible duplicate, but the other threads haven't solved my problem completely. Let's say I have the following classes A and B which extend BasicClass. public abstr...
Having being taught during my C++ days about evils of the C-style cast operator I was pleased at first to find that in Java 5 java.lang.Class had acquired a cast method. I thought that finally we ...
I have a Java project that uses an internal company library made in Kotlin. In this library, I have a Kotlin class that extends the Feign.Builder. This is how the decompiled code looks like public ...
I am trying to implement Lazy Singleton with Generics and Inheritance. I have created a abstract super-class and declared a Map which will store all the instances of the child class of this class. ...
I've found many uses for lazy evaluation, such as a tool for optimization (e.g. matrices). Another use is syntactic sugar. But before I go overboard and make my code look at lot cleaner at the cost...
I use a method to collect all classes from a package and save them into an array: Class[] classes = ClassUtils.getClasses("xyz.keahie.werewolf.character.characters"); They all have the class "Cha...
Developing application in C# .Net 4.5 using Xamarin, targeting Android. I have a custom class that has some properties in it. I am trying to use a built in component that does comparison using Java...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.