Reputation: 89
Is serializable inheritable. Particulary if I have
class A implements Serializable{}
class B extends A{}
Is class B serializable?
Upvotes: 3
Views: 253
Reputation: 16392
I agree that Serializable-ness is inherited, but have had problems (albeit rarely) where that fact is not recognized by the occasional tool. So even though it's not required, I will make the extra effort to also declare the subclass as implementing Serializable.
Upvotes: 0
Reputation: 310893
All interfaces implemented by base classes are inherited by their derived classes. Not just Serializable.
Upvotes: 0
Reputation: 68715
Yes. This follows from the concept of inheritance in Java. Since A is serializable and B extends A, B is serializalbe.
Note that all non-static non-transient fields of A and B must also contain Serializable objects, otherwise there will be a NotSerializableException at runtime when you try to serialize them.
Upvotes: 2
Reputation: 26528
If a class is Serializable then all subclasses are Serializable. But if you want to prevent sub class whose superclass is Serializable to be serializable it is also possible.
If you want to prevent then use
NotSerializableException -- Thrown when an instance is required to have a Serializable interface. The serialization runtime or the class of the instance can throw this exception. The argument should be the name of the class.
Refer official docs -- http://docs.oracle.com/javase/6/docs/api/java/io/NotSerializableException.html
By using it in this way you can accomplish this task
private void writeObject(ObjectOutputStream out) throws IOException {
throw new NotSerializableException(“Not today!”);
}
private void readObject(ObjectInputStream in ) throws IOException {
throw new NotSerializableException(“Not today!”);
}
How about sub class is serialized But parent class is not
If a Sub class is Serializable, it absolutely does not mean that its super class is also Serializable. When a subclass is de - serialized, the no-argument Constructor of the super class will run.
Upvotes: 0
Reputation: 5057
Did the following check on given code
boolean b = new B() instanceof Serializable;
System.out.println(b);
which returns true
Upvotes: 2
Reputation: 40982
Interfaces have another very important role in the Java programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. The Java programming language does not permit multiple inheritance (inheritance is discussed later in this lesson), but interfaces provide an alternative.
In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement. This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface. This is discussed later in this lesson, in the section titled "Using an Interface as a Type."
I guess what you mean Is if you have a function foo
which gets (Serializable ptr)
, can you call it with an instance of Class B
so the answer is yes, that is the Idea.
moreover you can Override the the A (ancestor) implementation.
Upvotes: 0
Reputation: 7076
Yes, if the super class implements serializable, then so do the sub classes.
Upvotes: 0
Reputation: 20155
Yes, subclass of a Serializable class is also serializable
For more information
http://docs.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html
http://java.sun.com/developer/technicalArticles/ALT/serialization/
Upvotes: 2