Bug 45919 - [PATCH] Crucial classes made inheritable: HSSFWorkbook, HSSFSheet, HSSFRow and HSSFCell
Summary: [PATCH] Crucial classes made inheritable: HSSFWorkbook, HSSFSheet, HSSFRow an...
Status: RESOLVED WONTFIX
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: unspecified
Hardware: All All
: P2 enhancement (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
: 51574 (view as bug list)
Depends on:
Blocks:
 
Reported: 2008-09-30 05:46 UTC by Thomas Guretzki
Modified: 2011-07-28 13:03 UTC (History)
1 user (show)



Attachments
Patch to make HSSFCell inheritable (6.61 KB, patch)
2008-09-30 07:46 UTC, Thomas Guretzki
Details | Diff
Patch to make HSSFRow inheritable (5.39 KB, patch)
2008-09-30 07:47 UTC, Thomas Guretzki
Details | Diff
Patch to make HSSFSheet inheritable (3.91 KB, patch)
2008-09-30 07:47 UTC, Thomas Guretzki
Details | Diff
Patch to make HSSFWorkbook inheritable (3.04 KB, patch)
2008-09-30 07:48 UTC, Thomas Guretzki
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Guretzki 2008-09-30 05:46:41 UTC
With these patches,
Comment 1 Thomas Guretzki 2008-09-30 06:41:01 UTC
[sorry for the break]
With these patches, it is possible to derive classes from HSSFCell and the linked classes HSSFRow, HSSFSheet and HSSFWorkbook.
This can be very useful if you want to use inherent references between these objects in your own code which uses additional attributes e.g. of cells. That way,  one does not have to create one's own ArrayLists, e.g. of the cells of every row. Plus you get all HSSFCell methods for free without any code. 

The main change to make this possible was to create working public constuctors in HSSFSheet, HSSFRow and HSSFCell. Before, there were only creator methods in the respective superior class, e.g. createRow in HSSFSheet. These continue to exists, but they simply call the respective constructors. There are some details beyond this concerning how to get the newly created e.g. HSSFCells into the administration of their HSSFRow, but you can look that up in the code.

If you extend only one of these classes, be aware that you _must_ use its constructor for newly created objects. For example, if you extend HSSFSheet, you call "new HSSFSheet (HSSFWorkbook, String)" and pass it an existing HSSFWorkbook and the name of the new sheet.
If you want to use the createSheet method of HSSFWorkbook, you have to extend that class and override that method, containing code like "result=new HSSFSheet (HSSFWorkbook, String)". 
The code contains hints like "##Override..." to quickly find the concerned methods.
Comment 2 Thomas Guretzki 2008-09-30 07:46:57 UTC
Created attachment 22651 [details]
Patch to make HSSFCell inheritable
Comment 3 Thomas Guretzki 2008-09-30 07:47:25 UTC
Created attachment 22652 [details]
Patch to make HSSFRow inheritable
Comment 4 Thomas Guretzki 2008-09-30 07:47:44 UTC
Created attachment 22653 [details]
Patch to make HSSFSheet inheritable
Comment 5 Thomas Guretzki 2008-09-30 07:48:11 UTC
Created attachment 22654 [details]
Patch to make HSSFWorkbook inheritable
Comment 6 Josh Micich 2008-10-01 22:26:27 UTC
From a functionality perspective, you are probably tryng to do something very sensible.  Can you please attach some sample code that shows your goal?

However, the technical change specified in these patches is contrary POI's current direction.  POI avoids allowing API classes to be extended because of the difficulties that this presents.  The trouble arises because inheritance is such an intimate form of coupling.  

The most discernible difficulty relates to maintaining of backward (source/binary) compatibility.  This link has some useful tables classifying potential changes:
http://wiki.eclipse.org/index.php/Evolving_Java-based_APIs

Much more subtle are the unexpected behaviour/logic consequences of inheritance.  See this page for some well explained examples:
http://norvig.com/java-iaq.html

Most code using unwanted inheritance can be rewritten using delegation and/or the 'strategy pattern'.

Don't get me wrong - inheritance isn't all bad.  POI uses *plenty* of inheritance, just internally. My personal opinion is that "you should never inherit from a class that you do not control the source code of".
Comment 7 Yegor Kozlov 2011-07-28 13:03:55 UTC
*** Bug 51574 has been marked as a duplicate of this bug. ***