[5865] in java-interest
Re: keyword to introduce a method?
daemon@ATHENA.MIT.EDU (garya@ics.com)
Fri Mar 1 05:24:59 1996
From: garya@ics.com
Date: Fri, 1 Mar 1996 02:39:34 -0700 (MST)
To: mspille@bear.com (Mike Spille), strong-java@entmp.org
Cc: java-interest@java.sun.com
>> Consider the following:
>>
>> public class Base {
>> ...
>> }
>>
>> public class Derived extends Base {
>> public int foo() {...}
>> }
>>
>> The class Derived introduces the method foo, which may be overridden
>> by any of Derived's subclasses, and whose semantics are defined by Derived.
>>
>> The author of Base may, at some later point in time, release a new
>> update which introduces the same function:
>>
>> public class Base {
>> public int foo() {... different semantic definition ...}
>> }
>>
>> This updated Base class silently changes the operation of Derived and all
>> its subclasses.
>>
>
>Not true!! In java, all methods are virtual. Thus, if you call foo() on
>an object which is really a Derived (or one of its children), then you'll get
>the Derived version of foo. If the author of Derived later on decides he wants
>to invoke the Base version of foo, he just does a super.foo().
You miss the point. Originally, there *isn't* a Base.foo, and Derived and all of
its subclasses which reference foo are doing so assuming they will get the
semantics as defined by the author of Derived. If Base suddenly introduces a foo
function, then Base's use of foo will incorrectly vector to all the Derived foos,
and nothing will work correctly because Base isn't working correctly.
>Finally, you don't have to worry about code "silently changing", because
>prior to the addition of foo() to Base, you couldn't call foo() on Base
>objects, only on Derived objects.
Not true. Assume Base has a function called bar, which derived classes use.
Originally, it had no references to foo; Base.foo didn't exist. The author of
Base decides to decompose the code further, splits out a piece of it, and
calls it foo. Now the bar function, which the derived classes use, or which
other users use, works fine in the case of a simple Base object. But for any
derived object they won't work properly, because Base.bar will call an incorrect
derived.foo.
Gary Aitken garya@ics.com (business)
garya@dreamchaser.org (personal)
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com