interface as a method parameter in Java

前端 未结 8 1868
一个人的身影
一个人的身影 2020-11-30 20:34

I had an interview days ago and was thrown a question like this.

Q: Reverse a linked list. Following code is given:

public class ReverseList { 
    i         


        
8条回答
  •  暖寄归人
    2020-11-30 20:48

    This is called programming to interfaces. You don't code to a specific implementation class of node lists but to the interface implemented by all those implementations.

    That way your code will still work if someone writes a new and much better implementation of NodeList after you wrote your reverse method and you don't have to adapt your code for each new implementation of NodeList.

提交回复
热议问题