Logo 
Search:

Java Answers

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds
  Question Asked By: Lisa Anderson   on Feb 17 In Java Category.

  
Question Answered By: Sonya Flores   on Feb 17

One way to implement this is using serialization,
because each constructor of a class  derived from Ship could can
different number of parameters.

I'm sending you an example, your Ship class is equivalent to
MiClaseSerializable, that class have to implement the interface
"Serializable" so Java is responsible to convert your class in
a serialized String without taking the attributes defined as
"transient", you can redefine the readObject method if you have
some transient attributes that you need to initialize each time
the object is readed and not created by the constructor, but
is not necessary.

In the Aplic class you can see how the program saves objects
("grabar" method) and read  them ("leer" method) from a text  file.
In the reading you can make reference to your superclass (Ship),
as i make reference to MiClaseSerializable, and because you will
read really Tug, a derived class from Ship, you wont have any
problem, that will be managed by poliformism.

In the example, I use a vector in the Aplic class, but you
can save and read directly your class, without using vectors,

Output

Share: 

 

This Question has 1 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Create class from a string Or get search suggestion and latest updates.


Tagged: