Re: multiple inheritance in Java
On 7/2/2013 5:49 AM, Stefan Ram wrote:
Eric Sosman <esosman@comcast-dot-net.invalid> writes:
So, just a struct?
Yes. Recently, I often had this thought, that sometimes
structs are appropriate.
Yeah, but they are references to objects with non-final fields.
If thread T1 does cp.color.setR(42) and T2 does r=pc.color.getR()
while cp and pc refer to the same ColorPoint instance (or to distinct
ColorPoint instances that refer to the same Color instance), you've
got a race.
How do you know the author of Color has not taken care for
proper synchronisation? IMHO, this is a question of thread-safety
of the Color class not of the ColorPoint class.
Not enough, because the constraint applies to both the
Color and Position classes simultaneously. Even if both of
those classes are individually thread-safe, the combination
isn't. If actions A1 and A2 are atomic, it does not follow
(in fact, almost never follows) that A1;A2; is atomic.
... and suddenly I've got constraints not anticipated by the
original author -- without changing a single line of his code.
With (overridable) accessors I could manage those constraints;
with exposed fields I could not.
I see, so I should declare the class final.
Right. Inheritance is for *your* benefit alone, and you
should make sure nobody else can use it. ;-)
--
Eric Sosman
esosman@comcast-dot-net.invalid