> Hello,
>
[quoted text clipped - 3 lines]
> network in a bigger program that I have written.
> You can forget about that detail.
But you can't. Pay attention to Tom Hawtin's advice about Serializable:
>> By making your class serialisable you are effectively opening up a second interface onto the class. Try to avoid exposing the implementation as much as practicable. So much grief had been caused by adding implements Serializable without thinking further. serialVersionUID should also be present.
Read Joshua Bloch's chapters on the subject in /Effective Java/. There is a
lot to take care of if you make a class Serializable.
> I take note on making the methods final, as they will remain that way
> on the classes that inherit this.
That is a good thing. Remember not to invoke overridable methods from
constructors.
> It is abstract because there will be no instance of this class.
>
> I need to create near 20 or 30 classes based on one abstract class.
>
> My reason to use inheritance is not to have to repeat 20 or 30 times
> the same code, so all becomes really confusing. Is this right?
Maybe. If your analysis indicates that the 20 or 30 types /are-an/ instance
of the supertype, then inheritance is the way to implement that. Re-use of
common base functionality is a strong but not compelling argument in favor of
using inheritance.
Note that code duplication can be prevented without inheritance by using
composition, instead including a helper-class object to handle the common
functionality. For example, most situations that call for a collection, say a
Map, should use a Map instance member rather than inherit or implement Map.
Again, this is a topic well covered in /Effective Java/.
> I need to set some variables each time I create the instances of
> subclasses. This may be done simply with constructors.
> I create the get and set methods for if I need to change some value .
>
> Do you think this is right?
If you make the methods final, most likely. However, there is an argument in
favor of not having the setX() method, making the instance variable final, and
simply creating a new instance of the object if you need a new value. For one
thing, it can make your code much more efficient. For another, it prevents
override abuse. For a third, it simplifies concurrency issues.
This is called making the class "immutable", or more correctly, making all its
instances "immutable". (The term is approximate - as others have pointed out
elsethread, nothing is ever completely "immutable" in Java.)
The trick is whether your design calls for the methods to be overridable.
Another topic thoroughly covered in /Effective Java/.

Signature
Lew
Lew - 20 May 2007 20:08 GMT
> The trick is whether your design calls for the methods to be overridable.
I am not sure what I meant by that. I was trying to say that whether to make
a class or some of its fields immutable is a matter of your analysis and the
essential nature of the type you're defining. It is always something to think
about.

Signature
Lew
Jordi - 20 May 2007 20:41 GMT
Thanks Lew.
I do need to serialize the class.
Maybe I don't need to implement Serializable in the abstract class,
but the subclasses will use it as I need to send them through a Object
Stream and then to a byte array thru NIO, as I have my system now
developed.
The classes I am writting are objects that contain data to be sent
thru the net, so all them need this.
If I don't make Serializable the subclasses, they won't work for what
I need.
I have not read that book. Is there some resume in internet?
Maybe I should remove the Serializable from the abstract class?
Thanks again.
Jordi R Cardona
Tom Hawtin - 20 May 2007 21:25 GMT
> I have not read that book. Is there some resume in internet?
The bullet points from that book [Effective Java] and some discussion
are here:
http://www.java.net/cs/user/forum/cs_disc/1768
It's worth getting, but note there is a new edition out at some point.
> Maybe I should remove the Serializable from the abstract class?
The base class will need to be serialisable if you expect it to look
after its own data. Subclasses can manage it by implementing readObject
and writeObject methods. However, if you use readFields/writeFields
(which would be the most natural way of storing the data), you run into
problems using final fields.
Tom Hawtin
Jordi - 21 May 2007 06:38 GMT
Thanks Tom for the link
I will read it and study.
Jordi