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

no my dear,
The composition is when you make a class which has members of other types.
Let say that we develop a reservation plane system.
You have, the class client, the class plane,
and the class tiket.
A composition is when you make the reservation object which has a tiket, a client, and a plane as the member variables.
Infact taking in acount design patterns the above situation is a agregation.
A composition is a tree structure.
Let say you have a company.
A company has employees.
The general point of view is to kmake a general class named person, or employee and subclass it with, manager, hourworker, comisionworker, and worker.
But this is not so necessary.
For example we can make a employee class and make it to has a collection of employees. So we distinguish a manager from a employee with the fact that a manager has the colection of employees with more than 0 e,lements.
so a composition in c# for this situation will look ;like:
public class Employee
{
private IList<Employee> subEmployees;
....//other code
}
Thinks about the two situation:
When you make a composition it means that object1 has a object2
when it is about inheritance that means object2 is a object1.
the key words are:
"has a"
and
"is a"

----- Original Message ----- From: <james.homme@xxxxxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Saturday, July 26, 2008 4:15 PM
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 http://www.freelists.org/list/programmingblind

Other related posts: