Reputation: 171
I'm quite new to Java and am stuck with this, please. Well I know that an interface 'provides' only abstract methods to be implemented by the concrete subclasses. Hope this is true (at least). So I was studying a sample code of jxl api, to write and read an excel file, and I arrived to this point:
workbook = Workbook.createWorkbook(new File("/home/me/workspace/files/output.xls"));
WritableSheet sheet = workbook.createSheet("first sheet", 0);
That is, the workbook object was previously instantiated and is a WritableWorkbook object. I'm creating the workbook using the static method provided by Workbook that is a superclass of WritableWorkbook. And until this, I'm there. But, then, what am I doing in the second instruction? The sheet is a "WritableSheet", but this is an interface! And seeing the api, there are no implementing classes. And "WritableSheet" is a subinterface of another interface, which is "Sheet". Both interfaces. But, I thought that I wasn't able to create an object from an interface. While, then, using the createSheet method (provided by WritableWorkbook), it seems I'm creating a WritableSheet. Then in the sample code I arrive to this:
Label lblNome = new Label(0, 1, "Nome:");
sheet.addCell(lblNome);
No problem the first line, I create a new element, a label (that is a text cell); but the second? again, addCell is a interface method seeing the api! while sheet was the previous implementation of what? Of an interface seems to me. And which method they are using, if interfaces provide abstract method?
Please would you clarify me that? At the end the code works, but it's not clear how those interfaces (Sheet and WritableSheet) are working.
Thank you
Upvotes: 1
Views: 1072
Reputation: 14549
Interfaces in Java are defining a contract and they're types! There may be several classes signing this contract - we say these classes are implementing the interface.
You can see any object created by a class that signed a contract as an object of this contract. It can be casted to the contract - we say you can cast any object from a class implementing an interface to this interface type.
The static factory method createSheet()
is doing sth like that. It is internally using a concrete class that is implementing WritableSheet
and returning an object of it. Since createSheet()
's return type is WritableSheet
it is casted to the type WritableSheet
. Be sure to comprehend that we did not create an object of WritableSheet
- we cannot since it is an interface. But we can cast an object of any class implementing the interface to the type WritableSheet
.
One major advantage is that the implementation used internally by createSheet()
can be changed silently without affecting client code that uses it. When createSheet()
switches over to another implementation that implements WritableSheet
then you do not have to change anything in your client code!
Your code relies on a contract defined by the interface rather than a concrete implementation. And that's good!
Upvotes: 0
Reputation: 691695
All objects are instances of concrete classes. Maybe you don't see any implementing class in the javadoc, but there is one. It's probably an anonymous or private class instance that is returned by createSheet
, like this:
public WritableSheet createSheet() {
return new WritableSheetImpl(...);
}
private class WritableSheetImpl implements WritableSheet {
// implementation of all the methods of WritableSheet here
}
Upvotes: 1