Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Overrideしてたらキャストしても親のメソッド呼べるわけじゃない事を確認するテスト
package toString;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import org.junit.Test;
public class ToStringTest {
class A {
@Override
public String toString() {
return "A";
}
}
class B extends A {
@Override
public String toString() {
return "B";
}
}
@Test
public void testOverrideToString() {
B b = new B();
assertThat(b.toString(),is("B"));
assertThat(((A)b).toString(),is("B"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment