• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces in Inheritance  RSS feed

 
senthil rajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Frnds,

class A is the parent class which implements Serializable interface. Class B is the child class which extends Class A.
Now my question is I don't want class B as Serializable, So can make class B as non Serializable class .. Is it possible?

I think one way is we can mark all variables as transient. Is there any other way?

Pls clarify me on this.

Thanks,
Senthil.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Class B IS-A class A. If A is Serializable then B must be Serializable. Marking all variables transient would be a worthless kludge. Not only would it most likely break class A's contract but it wouldn't prevent class B from being serialized, specifically it wouldn't change that B is Serializable nor would it prevent A's data from being serialized. All it would serve to do is break the serialization, which is about the worst solution I can imagine. If class B should not be Serializable then either class A shouldn't be either or class B is not a class A and shouldn't extend it.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by senthil rajan:
Hi Frnds,

class A is the parent class which implements Serializable interface. Class B is the child class which extends Class A.
Now my question is I don't want class B as Serializable, So can make class B as non Serializable class .. Is it possible?

I think one way is we can mark all variables as transient. Is there any other way?

Pls clarify me on this.

Thanks,
Senthil.


The best way to prevent serialization is to override the readObject() and writeObject() methods to throw a NotSerializableException. These two methods are used by the serialization process to serialize the object.

There should be a couple of examples on the internet that you can google for about this.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!