title: Polymorphism and casting object in Java
body: <p>is there any possibility that o1 or o2 can be casted to A and the program will run? Why is the last statement a runtime error?</p>
  <pre><code>public class A{
      public A(Object object)
      {
      
      }
      public A(){
      
      }
      public String toString(){
          return &quot;A&quot;;
      }
  }
  
    public class Main{
        public static void main(String []args){
  
            A a4 = new A();
            Object o1 = new Object();
            Object o2 = new A(o1);
      
            a4 = o1; //Compilation error
            o2 = o1;
            ((A)o1).toString();//Runtime error
            a4.toString();
            ((A)o2).toString();//Runtime error
  }
  </code></pre>
  <p>}</p>