I'm confused about Polymorphism

Posted by Vtanathip on Stack Overflow See other posts from Stack Overflow or by Vtanathip
Published on 2011-02-05T14:37:51Z Indexed on 2011/02/05 15:25 UTC
Read the original article Hit count: 143

Filed under:
|

I'm know polymorphism rule that we can send it via parameter like this code

interface Animal {
  void whoAmI();
}

class A implements Animal{

    @Override
    public void whoAmI() {
        // TODO Auto-generated method stub
        System.out.println("A");
    }

}

class B implements Animal{

    @Override
    public void whoAmI() {
        // TODO Auto-generated method stub
        System.out.println("B");
    }

}

class RuntimePolymorphismDemo {

public void WhoRU(List t){
    System.out.println(t.getClass());
}

public static void main(String[] args) {
     A a = new A();
     B b = new B();

     RuntimePolymorphismDemo rp = new RuntimePolymorphismDemo();
     rp.WhoRU(a);
     rp.WhoRU(b);
  }
}

but

 List<Example> examples = new ArrayList<Example>();

This code,I'm don't understand why we must use List.

why we can't use like this?

ArrayList<Example> examples = new ArrayList<Example>();

Because when we use List we can't use method that only have in ArrayList class like trimToSize()

and How I know when to use or not use?

© Stack Overflow or respective owner

Related posts about java

Related posts about oop