1

Related Question:

Why is java.util.Observable not an abstract class?

Since we have interfaces which can contains default methods, isn't it a better idea to change Observable to an interface? From a functionality point of view, the Observable "does a thing" but not "is a thing". It should be changed to an interface in Java 8 correct?

Community
  • 1
  • 1
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
  • Although it's not an answer, and although Java 9 is not final yet; but in Java 9 both java.util.Observer and java.util.Observable are deprecated. – Mohamed El-Beltagy May 09 '17 at 17:52

3 Answers3

9

It should be changed to an interface in Java 8 correct?

Not unless you want to break all backwards compatibility, no. If you changed it to an interface, then anything written as:

public class Foo extends Observable

would be broken. I suspect it may well also be invalid in terms of binary compatibility too, but just source incompatibility would be enough to make it a no-go change, IMO.

Likewise:

  • It's currently a concrete class, not an abstract class... so new Observable() is currently valid but wouldn't be as an interface
  • As you say yourself in another answer, the default implementation uses state... so it can't be implemented by interface default methods
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    More to it, code like `new Observable()` would break as well, if `Observable` becomes an `interface`. And yes, it breaks binary compatibility as well, the differentiation between classes and interfaces happens within the byte code as well. – Holger Nov 10 '14 at 09:43
  • @Holger: Ah - I hadn't realized it was already a concrete class. I thought it was abstract. Will add this. – Jon Skeet Nov 10 '14 at 09:45
  • I wasn’t aware of it as well and it’s of little use because of the `protected` methods but that was discussed in the linked question so I learned that in principle it is possible and you never know what coders do. So it’s clear that Oracle won’t break compatibility even with obscure code usage if there is no justifying benefit. – Holger Nov 10 '14 at 09:50
7

It will break backwards compatibility, and all classes that extend Observable will be broken.

nunofmendes
  • 3,731
  • 2
  • 32
  • 42
AlexR
  • 114,158
  • 16
  • 130
  • 208
2

I did not look into it for too much so did not realize that

even in Java 8, interfaces cannot be state-ful.

An Observable requires a change flag and an array of Observers to be stored inside and thus stateful, and Java 8 does not support it.

SwiftMango
  • 15,092
  • 13
  • 71
  • 136