RE: Java - Help Understanding Advice From Learning Java Third Edition

  • From: "Ken Perry" <whistler@xxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Sat, 26 Jul 2008 21:18:55 -0700


Ok what I call this is a book written by someone who knows what they are 
talking about but not exactly how to get it across to people just getting 
started.  I am not even sure I can do it justice but I will give it a shot by 
using examples.  Using objects by composition means your going to include the 
object in your class like you would any other variable.  So for example in a 
class you might have an int to store integers.  In the same way you might have 
a TreeView to store items that you want shown on the screen.  If you have a 
variable in your class like a TreeView in your class you are using it as a 
composed object or a member variable.  

Now if you have a class that you want to be a more advanced TreeView you will 
inherrit a TreeView and add to it in your class.  So the Treeview is not a 
member variable instead it’s a parent of your AdvancedTreeView.  

What this guy is trying to say is it is easier to use a persons class as a 
composed object         when you can because you don't have to add 
functionality to the base class you just use what is there.  Also if for 
example someone upgrades the Treview class and you used it as a composed object 
it should not break your code by much and should be easily fixable.  Now if 
they update a base class and you had a lot tied into into the parent you might 
run into trouble for example

Lets say you used a TreeView class as a composed object.  And did something like

TreeView.AddItem("frog");

Most likely the creator of the treeview when changing the class will leave the 
AddItem function the same but now he has decided to store all items in his tree 
view in a hash instead of the list that he used first.  I know he would be 
stupid to do either just use it as an example.  So he changes the base member 
variable from List MyList.  To Hash MyHash.  

If you had inherited the TreeView to make a better TreeView and had used the 
parents MyList variable in hundreds of things you were doing you would have to 
go through and fix it.  While if you composed it and only used the methods like 
AddItem that the creator intended you to use you would not even notice he or 
she had made a change.

Anyway I am sure I am rambling now so if I did no better then this guy I am 
sorry and let me know I didn't and maybe I will come up with a better example.  
If you think code does better then I could write you an example of a class that 
uses composed objects all though I am betting you used composed objects all the 
time for example in Java the String class is a object that you use all the time 
as a composed object for example this is a composed string

String myString="tada";


If on the other hand you needed to have a special  function in the string class 
you would have to inherit the string class and add a method to it to lets see 
hmm a scramble all the letters.  Then once you did that everyone could use your 
class just like a String class but with the added functionality of scramble.

Anyway I will stop now if you still don't have it let me know and I will try 
again.  I will say though this is where I banged my head back in 1992 when I 
decided to learn c++ over c.  After you get over things like Inheritance and 
virtual functions you will be in the clear.

Ken

-----Original Message-----
From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of 
james.homme@xxxxxxxxxxxx
Sent: Saturday, July 26, 2008 6:16 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Java - Help Understanding Advice From Learning Java Third Edition


Hi,
The following piece of text comes from the first chapter of this book, which 
starts to talk about classes. I feel that it's important for me to grasp what 
it's talking about, but I don't understand what it's trying to tell me. Can 
anyone say it a different way and possibly help me understand what it's driving 
at?

Specialize objects only when you have to—use composition instead of inheritance 
. When you use an object in its existing form, as a piece of a new object, you 
are composing objects. When you change or refine the behavior of an object (by 
subclassing ), you are using inheritance . You should try to reuse objects by 
composition rather than inheritance whenever possible because when you compose 
objects, you are taking full advantage of existing tools.
Inheritance
involves breaking down the barrier of an object and should be done only when 
there's a real advantage. Ask yourself if you really need to inherit the whole 
public interface of an object (do you want to be a "kind" of that object) or 
whether you can just delegate certain jobs to the object and use it by 
composition.

I feel almost like they are saying that you should import the code from the 
objects you want to use into your program and make entirely new classes that 
use the imported ones. Am I on the right track?

Thanks.

Jim

James D Homme, Usability Engineering, Highmark Inc., james.homme@xxxxxxxxxxxx, 
412-544-1810

"it is only possible to live happily ever after on a day-to-day basis." -- 
Margaret Bonnano

Highmark internal only: For usability and accessibility:
http://highwire.highmark.com/sites/iwov/hwt093/Vjʱjín

__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: