• Post Reply Bookmark Topic Watch Topic
  • New Topic

Generics and add method of ArrayList  RSS feed

 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if I understand the following code properly:

The reference is of the type List, the object is of the type ArrayList. Why can I use the medthod add(E e)? Let me try an explanation. The reference birds is of the type List, the interface List declares a method boolean add(E e), according to the API the method boolean add(E e) returns true (as specified by Collection.add(E)). The class ArrayList has a method boolean add(E e) too, it is specified by add in interface List<E>. Thus the class ArrayList does not have its own method boolean add(E e), in the code above the interface List must have a method add, otherwise it would not work.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The interface is a contract with all classes that implement it to provide concrete methods as spelled out in the interface. List declares add(E e) but does not provide an implementation of it. ArrayList implements the List interface and therefore must supply a concrete add(E e) method, not just declared, but defined.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a real-world analogy for you: We all know that a car has at least a steering wheel, a gas pedal, and a brake pedal. These are the basic elements that allow us to control a car, right?  The idea of a "car" having all these characteristics is like the idea of the List interface specifying a method add by which an element can be added to any implementation of List.  The idea is abstract.

Now, if you get into any specific car, like a Toyota Corolla, a Ford Mustang, a Jaguar XJS, a Porsche 911, a Chevy Camaro, or whatever other modern car you can think of, all of these cars will likely have a steering wheel, a gas pedal, and a brake pedal. All these specific cars are implementations of the general concept of a "Car" just as an ArrayList is a specific implementation of the general contract that is defined by the List interface.  Anyone getting into a Toyota Corolla can expect to be able to control it in essentially the same way they would a Mustang or a Porsche or whatever car, not considering the differences in performance, of course.

The concept of a "Car" doesn't have an actual steering wheel or gas pedal because it's just an idea, right? It's abstract.  In a very similar way, the List interface is also abstract, so it doesn't have an actual add() method implementation since it's just a specification that its implementations must comply with.  Only the actual physical car instances have a physical steering wheel and gas pedal, just as only the actual implementations of List like ArrayList have the actual add() method.  Each concrete implementation of List will have their own add() method implementation, just as each physical car has its own actual steering wheel and gas pedal.

Does that make sense?
 
Dana Ucaed
Ranch Hand
Posts: 406
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Here's a real-world analogy for you: We all know that a car has at least a steering wheel, a gas pedal, and a brake pedal. These are the basic elements that allow us to control a car, right?  The idea of a "car" having all these characteristics is like the idea of the List interface specifying a method add by which an element can be added to any implementation of List.  The idea is abstract.

Now, if you get into any specific car, like a Toyota Corolla, a Ford Mustang, a Jaguar XJS, a Porsche 911, a Chevy Camaro, or whatever other modern car you can think of, all of these cars will likely have a steering wheel, a gas pedal, and a brake pedal. All these specific cars are implementations of the general concept of a "Car" just as an ArrayList is a specific implementation of the general contract that is defined by the List interface.  Anyone getting into a Toyota Corolla can expect to be able to control it in essentially the same way they would a Mustang or a Porsche or whatever car, not considering the differences in performance, of course.

The concept of a "Car" doesn't have an actual steering wheel or gas pedal because it's just an idea, right? It's abstract.  In a very similar way, the List interface is also abstract, so it doesn't have an actual add() method implementation since it's just a specification that its implementations must comply with.  Only the actual physical car instances have a physical steering wheel and gas pedal, just as only the actual implementations of List like ArrayList have the actual add() method.  Each concrete implementation of List will have their own add() method implementation, just as each physical car has its own actual steering wheel and gas pedal.

Does that make sense?


Superb post!
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is fine. My question is rather probably about polymorphism. What is the difference between these two codes:
The type of the reference is different. What does that mean?
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Search for program to the interface. In the first case that is what you are doing; it is easy to change a List to an ArrayList, LinkedList, or your own custom List implementation as required. In the second case you are programming to the implementation, specifying that your reference can only be an ArrayList. In this particular case, there are two methods: 1 2 which are not in the List interface, but that sort of thing is an unusual occurrence.

Declaring your List as ArrayList instead is equivalent to Junilu's declaring his Car as a BMW. As Junilu said, all cars have a steering wheel and accelerator pedal, etc. There are differences in the interface; some cars have a gear lever and a clutch pedal and some don't. Mercedes used to make a completely different sort of parking brake. Rather than its being a handbrake pulled by a lever between the two front seats, like in most cars, it was pulled on with a small pedal left of the clutch pedal and released by a knob on the dashboard. You can look on that as a different kind of implementation of the handbrake() method. The modern parking brake which is turned on by the little switch under the dashboard is another different implementation of handbrake().
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!

Similar Threads