Reputation: 6367
If I inherit from a class that is serializable but I specifically do not want my class to be serializable; what's the best way to strictly prevent serialization?
If there was a method in java.io.Serializable maybe I could throw an exception, but Serializable is empty.
Upvotes: 5
Views: 1381
Reputation: 38526
From the documentation
Serialization of an object can be prevented by implementing writeObject and readObject methods that throw the NotSerializableException. The exception will be caught by the ObjectOutputStream and abort the serialization process.
Upvotes: 7