Reputation: 1
After reading several books on Java inheritance I am still unclear on how Java's interfaces work. For example the Collection interface contains many methods such as "add" that is inherited by other interfaces such as the List interface. Since add is inherited from an interface, by definition the Collection interface will only provide a method signature for add. So my question is where is the concrete implementation of add defined (the code that actually does the adding ?)
Upvotes: 0
Views: 517
Reputation: 338496
An interface in Java is not about code, nor behavior (we will ignore default methods in this Answer). An interface is simply, and importantly, a contract. An interface proposes a set of method signatures. A method signature is a name of method along with arguments of certain types and a return value of a certain type.
The programmer for a class decides if she wants her class to promise and fulfill that contract. She labels her class with implements
to make the promise. And she writes methods in that class with the same signatures to fulfill the contract. The compiler verifies her work, making sure that each method signature listed in the interface is in fact implemented in the class.
So there is no inheritance with an interface.
You said:
such as the List interface. Since
add
is inherited from an interface
No, no inheritance involved. The List
interface is a contract promising that every implementing class will offer certain methods. One of those methods is add
. All implementations must have an add
method. So ArrayList
has an add
method. And LinkedList
has an add
method. Those two concrete classes both promise to fulfill the contract spelled out in the List
interface. And we know they fulfilled that contract, because otherwise the source code for those classes would never have been successfully compiled.
You asked:
where is the concrete implementation of
add
defined (the code that actually does the adding ?)
Each concrete class implementing the interface contains the actual code for implementing each method defined in the interface. Or, the concrete class inherits that code from its superclass. But an interface contains no code (again, let’s ignore default methods), so we know the code to do the adding is not in the interface.
The ArrayList
class implements the add
method by manipulating arrays behind the scenes. If the currently used array has a slot open, the element to be added is assigned to the slot. If the current array is full, the class tries to grow the array in contiguous memory by some number of additional slots. If successful, the element is assigned to the first of those added slots. If enough contiguous memory is not available, a new larger array is established somewhere else in memory, the old array’s contents are copied over, and finally the new element is assigned, while the old array is discarded. All this code in the ArrayList
class. The LinkedList
class has an entirely different approach to tracking and adding elements. So LinkedList
has an entirely different implementation of the add
method.
Upvotes: 2
Reputation: 2701
Create in IDE:
ArrayList list = new ArrayList();
list.add("hello");
Then hold the Ctrl and move cursor over the method 'add' and click. You will see implementation in ArrayList. Otherwise you can make right click on method add in interface List -> Go to-> Implementations and then select e.g. ArrayList.
Upvotes: 0
Reputation: 69
Any concrete implementation of Collection interface should provide an implementation for add method. For example, java.util.ArrayList provides implementation for add method.
Upvotes: 3