aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Modifying vs inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Modifying vs inheritance" Watch "Modifying vs inheritance" New topic
Author

Modifying vs inheritance

Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
I am done with my application, but as I think about some of the questions that we are asked to explain I draw a blank. Specifically, the modification or extending issue is a tough one. I modified the Data class. My thought for this is that the three methods that are being added should truly be a part of the base Data class rather than a subclass. My argument is pretty weak right now...some discussion on this subject would be greatly appreciated.
Thanks.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
A keyword in good OO design is "responsibilities". In a good design, each class has a small, well-defined, highly coherent set of responsibilities. To illustrate this, compare the following two statements:
"The Data class is responsible for providing access to a single local database file, comprising reading, writing and searching facilities."
"The Data class is responsible reading, writing and searching a single local database file, managing record and database locks for remote mode operation, and generating client IDs for remote clients."
The IS-A relationship implied by inheritance generally should be visible in these responsibilities as well. A subclass that acquires whole new responsibilities often should not be a subclass in the first place.
"RemoteData is a wrapper around Data which implements the same interface (DataInterface), but changes Data's local, single-user semantics into networked, multi-user semantics by (among other things) implementing locking with the help of a LockManager class."
"The RemoteData class is a subclass of Data responsible for providing access to a single local database file, comprising reading, writing and searching facilities, managing record and database locks for remote mode operation, and generating client IDs for remote clients. It is a Remote object to be exposed using RMI. The lock methods inherited from Data that do not take a client ID should not be used."
Finally, I'll be a bit more specific. Take criteriaFind(). If you adopt, say, the very first set of responsibilities for Data outlined above, it is easy to argue that criteriaFind() should go into Data itself as it is clearly part of Data's job description ("searching"). This decision is not true until the end of time! In the hypothetical case that you would later add indexing to the database, it is likely that the responsibility for indexing and searching would become large and unwieldy and would need to be split up. The code would be refactored along similar lines and (the implementation of) criteriaFind would probably migrate out of Data. But as things stand it is fine inside Data itself.
Hope this helps.
- Peter
[This message has been edited by Peter den Haan (edited November 24, 2001).]
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
Thanks for the help...it really makes a lot of sense. To associate the locking with a subclass of Data I think is the way to go.
Thanks.
reena kadam
Greenhorn

Joined: Nov 16, 2001
Posts: 16
Peter,
your explanation is really very deep and clear.
I was about to pack my project but now after reading your explanation I got stuck.
Originally posted by Peter den Haan:
[B]
"The RemoteData class is a subclass of Data responsible for providing access to a single local database file, comprising reading, writing and searching facilities, managing record and database locks for remote mode operation, and generating client IDs for remote clients. It is a Remote object to be exposed using RMI. The lock methods inherited from Data that do not take a client ID should not be used."
-----
Exaclty you mean to say RemoteData class(remote object) will extend Data class ? if that is true then my design is really wrong. u saved me. pl. comment on my design :
Data class implements dataInterface
RemoteData class extends UnicastRemoteObject, implements DataInterface
I have literally replicated Data class methods in Remotedata class(cut-paste), except RemoteData class is throwing RemoteException and implementing lock and unlock methods.
Is this correct or not proper OO design. Can u pl. comment (and guide)?
reena K
[This message has been edited by reena kadam (edited November 25, 2001).]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
The quotes above are not recommendations. All I suggest is that you contrast each pair of quotes and decide for yourself which one you think represents better design. I have my opinions, of course. Your RemoteData class represents the third quote rather than the fourth. I think that's fine. Stop worrying
- Peter
[This message has been edited by Peter den Haan (edited November 26, 2001).]
reena kadam
Greenhorn

Joined: Nov 16, 2001
Posts: 16
thankx peter.
reena K.
Gennady Shapiro
Ranch Hand

Joined: Sep 25, 2001
Posts: 196
I just had to say that extension does not necessarily mean 1 level extension.
Consider: Data -> SearchableData -> SearchableRemoteData (as opposed to Data-> RemoteData)
The chain of responsibility is clearer:
Data: basic data IO
SearchableData: search algorithm(s)
RemoteSearchableData: thread safety (locking)
By polymorphically using the last 2 objects you provide specialized behavior for local mode (no locking) and remote mode (do locking), which is one of the benefits of inheretance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Modifying vs inheritance