Is there more to an interface than having the correct methods

后端 未结 17 887
小鲜肉
小鲜肉 2020-11-22 13:37

So lets say I have this interface:

public interface IBox
{
   public void setSize(int size);
   public int getSize();
   public int getArea();
  //...and so          


        
17条回答
  •  自闭症患者
    2020-11-22 14:34

    A great example of how interfaces are used is in the Collections framework. If you write a function that takes a List, then it doesn't matter if the user passes in a Vector or an ArrayList or a HashList or whatever. And you can pass that List to any function requiring a Collection or Iterable interface too.

    This makes functions like Collections.sort(List list) possible, regardless of how the List is implemented.

提交回复
热议问题