fork download
  1. class A {
  2.  
  3. public void m1(){
  4. mx();
  5. }
  6.  
  7. public static void main(String[] args){
  8.  
  9. try {
  10. A a = ( B) new C();
  11. a.m1();
  12. B b = (B) new A();
  13. b.m1();
  14. } catch (Exception e) {
  15. e.printStackTrace(System.out);
  16. }
  17. }
  18.  
  19. public void mx(){
  20. System.out.print(10);
  21. }
  22. }
  23.  
  24.  
  25. class B extends A{
  26. public void mx(){
  27. System.out.print(30);
  28. }
  29. }
  30.  
  31. class C extends B {
  32. public void mx(){
  33. System.out.print(40);
  34. }
  35. }
Success #stdin #stdout 0.03s 711168KB
stdin
Standard input is empty
stdout
40java.lang.ClassCastException: A cannot be cast to B
	at A.main(Main.java:12)