title: Downcasting in Java
body: <p>Upcasting is allowed in Java, however downcasting gives a compile error. </p>
  
  <p>The compile error can be removed by adding a cast but would anyway break at the runtime. </p>
  
  <p>In this case why Java allows downcasting if it cannot be executed at the runtime?<br>
  Is there any practical use for this concept? </p>
  
  <pre><code>public class demo {
    public static void main(String a[]) {
        B b = (B) new A(); // compiles with the cast, 
                           // but runtime exception - java.lang.ClassCastException
    }
  }
  
  class A {
    public void draw() {
      System.out.println("1");
    }
  
    public void draw1() {
      System.out.println("2");
    }
  }
  
  class B extends A {
    public void draw() {
      System.out.println("3");
    }
    public void draw2() {
      System.out.println("4");
    }
  }
  </code></pre>