title: Java Polymorphism: Please Help Me to Understand Polymorphism
body: <pre><code>class ClassA {
      String whoAmI() {
          return "ClassA";
      }
  }
  
  class ClassB extends ClassA{
      String whoAmI() {
          return "ClassB";
      }
  }
  
  class Main {
  
      public static void main(String[] args) {
  
          ClassA obj1 = new ClassA();
          ClassA obj2 = new ClassB();
  
          System.out.println(obj1.whoAmI()); 
          System.out.println(obj2.whoAmI());
      }
  }
  </code></pre>
  
  <p>Output of the code above is:</p>
  
  <pre><code>ClassA
  ClassB
  </code></pre>
  
  <p>In the code above output is as expected that I can see the methods of class "ClassB" when I am creating a reference variable of class "ClassA" and instantiating with "new ClassB();", but why it is not same in case when I creating the reference variable of interface "List" and instantiating it by using its implementing class "ArrayList" (I know that we can't create objects of interfaces directly)?</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  public class Main {
  
      public static void main(String[] args) {
  
          List objList = new ArrayList();
  
          objList.add("One");
          objList.add("Two");
          objList.add("Three");
  
          System.out.println(objList.get(1)); // Only able to call methods of interface "List", but not methods of its implementing class "ArrayList. WHY?"
      }
  
  }
  </code></pre>
  
  <p>If class "ArrayList" is implementing interface "List" then why I am not able to call methods of class "ArrayList"? </p>
  
  <p>I know that there is some misunderstanding in my mind about the concept of Polymorphism. Please help me to clear that misunderstanding!</p>