"That means, we need to be able to change the class definition using an object of that class."
Really? You don't change the values in the fields of an instance of a class by changing the classes definition. The class definition is ... the source code / compiled bytecodes.
But if you DO change the class definition, you are liable to find that the new version of the class cannot deserialize instances that were serialized using the old version of the class. (Details depend on the precise nature of the changes ...)
More generally, Java serialization is not a good option for databases:
- it is slow,
- it is not incremental (if you are loading / saving the entire database)
- it doesn't scale (if you are loading the entire database into memory)
- it doesn't support querying (unless you load the entire database into memory and implement the queries in plain Java), and
- you run into lots of problems if you need to change the "schema"; i.e. the classes that you are serializing / deserializing.
But modulo those limitations, it will work; i.e. it is "achievable".
And if you are really talking about how to change the state of your in-memory Database
object, or how to create a new copy from an existing out then:
- implement / use getters, setters and / or other methods in the
Database
class,
- implement a copy constructor, factory method or
clone
method that copies the Database
object state ... to whatever depth you need.
All very achievable (simple "Java 101" level programming), though not necessarily a good idea.