6

If a java class, say classA, is extending classB, it cannot extend Observable. But if the setChanged() function were public, instead of protected, we can use composition to create an instance of Observable in classA.

I wonder what was the thought process behind making setChanged() protected.

1 Answers1

4

If setChanged and such were public, then anything could change them. Only the object itself should be able to change them.

To handle your case of ClassA that extends ClassB, you'd probably want to have a method on ClassA that returns an Observable; within ClassA, the observable is an inner class that extends Observable and which only ClassA has access to.

This is very much the way collections and Iterator work.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875