• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why to implement Serializable interface for all the classes in class hierarchy  RSS feed

 
Ilia Prikhodko
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

In my understanding, if

A extends B
B extends C
C extends D
and
D implements E
then A is instance of E.

I faced code where all the classes, independently on their place in class hierarchy were implementing Serializable interface (e.g. A extends B implements Serializable, B extends C implements Serializable etc.) I don't quite understand why do I need to implement Serializable interface for the class if one of its ancestors already implements it.

Can anyone suggest if there is any reasonable cause for this?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then A is instance of E.

It's the other way around, E is an instance of A: a dog is an instance of an animal, but an animal is not necessarily an instance of a dog.

why do I need to implement Serializable interface for the class if one of its ancestors already implements it.

It's not necessary, but it makes it clearer to someone who looks at the source code (or the javadocs) that objects of that class are supposed to be serializable.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
then A is instance of E.

It's the other way around, E is an instance of A:

No, I think Ilia was right the first time - see the original post.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilia Prikhodko wrote:
Can anyone suggest if there is any reasonable cause for this?


Well, obviously, as you shown, you don't *need* to do it. And it doesn't really make a difference to declare "implements" on an interface that has already been implemented.... but one possible reason that it is done is for clarity. You can quickly see it via the source code, or Javadoc.

Henry
 
Ilia Prikhodko
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Thanks a lot for your answers.

Topic can be closed.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it isn't closed. People are getting confused because you used meaningless titles for your classes. Don't write A B C. Write
Spaniel extends Dog
Dog extends Mammal
Mammal extends Animal

Animal implements Alive

Then the answers will be obvious.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Earlier, I wrote: . . .
Spaniel extends Dog
Dog extends Mammal
Mammal extends Animal

Animal implements Alive

Then the answers will be obvious.
Then I read your question properly!

If you change Alive to Serializable, you will see that all those class implement Serializable inherited from Animal.
 
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
Campbell Ritchie wrote:Then I read your question properly!

If you change Alive to Serializable, you will see that all those class implement Serializable inherited from Animal.


I believe the OP understood this. The OP was questioning the need for code that looks like this...

Spaniel extends Dog implements Alive
Dog extends Mammal implements Alive
Mammal extends Animal implements Alive

which unfortunately was buried in the description paragraph.

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