

But we can cast an object of any class implementing the interface to the type WritableSheet. Be sure to comprehend that we did not create an object of WritableSheet - we cannot since it is an interface. Since createSheet()'s return type is WritableSheet it is casted to the type WritableSheet. It is internally using a concrete class that is implementing WritableSheet and returning an object of it.

The static factory method createSheet() is doing sth like that. It can be casted to the contract - we say you can cast any object from a class implementing an interface to this interface type. You can see any object created by a class that signed a contract as an object of this contract. 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. 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. While sheet was the previous implementation of what? Of an interface seems to me.Īnd which method they are using, if interfaces provide abstract method? No problem the first line, I create a new element, a label (that is a text cell) īut the second? again, addCell is a interface method seeing the api! Then in the sample code I arrive to this: Label lblNome = new Label(0, 1, "Nome:") While, then, using the createSheet method (provided by WritableWorkbook), it seems I'm creating a WritableSheet. But, I thought that I wasn't able to create an object from an interface. And "WritableSheet" is a subinterface of another interface, which is "Sheet". The sheet is a "WritableSheet", but this is an interface! And seeing the api, there are no implementing classes. And until this, I'm there.īut, then, what am I doing in the second instruction? I'm creating the workbook using the static method provided by Workbook that is a superclass of WritableWorkbook. That is, the workbook object was previously instantiated and is a WritableWorkbook object. WritableSheet sheet = workbook.createSheet("first sheet", 0)

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")) Well I know that an interface 'provides' only abstract methods to be implemented by the concrete subclasses. I'm quite new to Java and am stuck with this, please.
