What is the benefit of polymorphism using Collection interface to create ArrayList object?

后端 未结 8 1526
情书的邮戳
情书的邮戳 2020-11-27 19:37

I studied polymorphism and understand that it can do dynamic method binding like below.

Assuming that class Animal is abstract class.

public class An         


        
8条回答
  •  臣服心动
    2020-11-27 19:54

    Collection is a supertype of ArrayList. If you only need the functionality provided by Collection, it's good practice because you're explicitly indicating what functionality you need in the variable declaration. That you choose an ArrayList in the initialization is irrelevant (though a good default choice); the declaration that it's a Collection tells you and any future coder exactly what contract you care about.

提交回复
热议问题