subclass of a serializable class needs to be serializable?
If I have 3 classes say A and It Implements java.io.Serializable . And I have B ,which is subclass of A. If I want to serialize B , B also implements java.io.Serializable. If not Why HttpServlet implements java.io.Serializable as GenericServlet already implements it. Please clarify me .
开发者_高级运维Thanks in Advance Raj
From the Java docs:
All subtypes of a serializable class are themselves serializable.
Technically, not necessary.
Note that Serializable
is just a marker interface. So, by explicitly implementing it, you're basically saying (marking) that you are aware of the Serializable restrictions/contract and designed accordingly.
As far as I can see, it doesn't need to implement java.io.Serializable. Maybe it's simply being done for the sake of clarity.
As pointed out by other folks, it does not have any technical implications (HttpServlet
implementing java.io.Serializable
). Its more from the clarity point of view which allows a developer to quickly view if this class is serializable or not. Its there all around JDK. Checkout the java.lang.Number
and its subclasses.
Its similar to best practices of importing packages wherein we should avoid wildcards (java.io.*). Technically, there is nothing wrong is using wildcard (unless there are conflicting classnames) but importing classes with fully qualified classnames is cleaner,
精彩评论