1

This is a homework assignment that I've been researching for over 2 hours and I still can't get a decent answer, so I would appreciate any help.

Suppose that Bicycle is a subclass of Vehicle.

Is the parameterized interface List<Bicycle> a subinterface of List<Vehicle> ? Explain briefly.

My issues are:

  1. I thought you couldn't even have lists in interfaces, only methods.

  2. How can a list be an interface, let alone a subinterface?

  3. What is this question even asking, in plain English? :/

Thanks in advance!

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
Jona
  • 1,023
  • 2
  • 15
  • 39
  • You should read covariance and contravariance in C# by Eric Lippert: http://blogs.msdn.com/b/ericlippert/archive/2007/10/16/covariance-and-contravariance-in-c-part-one.aspx – Craig Gidney Mar 04 '14 at 05:46
  • Also, http://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-arent-javas-generics-implicitly-p might help. Plus, note that `List` is indeed an [interface](http://docs.oracle.com/javase/7/docs/api/java/util/List.html). – Dennis Meng Mar 04 '14 at 05:46
  • But the question is asking whether one is a subinterface of another, not whether they are equal. I read through the links you posted and they explained the latter well (why list bicycle isn't the same as list vehicle), but it doesn't say whether it is a subinterface – Jona Mar 04 '14 at 05:51
  • Hold on. The SO question I linked to pointed out why `List` isn't a subclass of `List`, not just why they aren't the same. The hope was reading that would clear up why `List` isn't a subinterface. – Dennis Meng Mar 04 '14 at 05:58

2 Answers2

1

Regarding your issues

  1. You are not HAVING a List in an interface, List itself is an interface.

  2. List of course IS an interface (LinkedList and ArrayList being the classes implementing it). List just provides methods enough to describe a simple list, and ArrayList and LinkedList provide bodies of those method according to the implementation.

  3. To explain the actual question, I'll jot down some facts first. (Sorry if I'm going too basic, but I'm doing it for the sake of completion.)


Regarding the question

  • Subinterface means the interface that extends another interface.
  • Parametrized interface is the interface which take a class type as a parameter while creating a reference. e.g. List<Vehicle> means List is a parametrized interface which has been given Vehicle as it's parameter. (Here it means each element of List is a Vehicle. i.e. it's a list OF vehicles).
  • The question is asking whether a parameterized interface List<Bicycle> is a subinterface of List<Vehicle>. (i.e. if List<Bicycle> extends List<Vehicles> given the fact that Bicycle extends Vehicle.

I suppose it should be enough for you to understand the question and partly the answer too..

Good luck.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45
0

Is the parameterized interface List<Bicycle> a subinterface of List<Vehicle> ? Explain briefly.

No. The List interface is defined for generic types and the defined methods do not change for various type parameters. That is to say, List<x> has the same methods for any x.

Your issues are:

  1. I thought you couldn't even have lists in interfaces, only methods.
  2. How can a list be an interface, let alone a subinterface?
  3. What is this question even asking, in plain English? :/

My answers are:

  1. An interface can have fields (Math.PI), methods, and member classes (SomeParcel.Build). A method can have List as the return type, so a List can be in an interface (e.g Arrays.asList() ).
  2. List is only an interface. It is a subinterface of Collection and Iterator because List's superinterfaces are Collection and Iterator. In other words, List has all the methods of the superinterface Collection and more, therefore it is a subinterface of Collection.
  3. Does List<Bicycle> have more methods than List<Vehicle>?

I may be wrong, so I don't mind if someone wants to teach me about the finer nuances of Java terminology.