interface as a method parameter in Java

前端 未结 8 1858
一个人的身影
一个人的身影 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:53

    You cannot create an instance (/object ) of an Interface. Yes, you can pass Interface as a parameter in the function. But the question seems incomplete. Interface isn't implemented by any class. Something is missing. If you try to run this, compiler will not show any error.

    But, in the reverse() method you need to create an instance of class that implements NodeList interface. I hope this makes sense.

提交回复
热议问题