[5883] in java-interest
Re: keyword to introduce a method?
daemon@ATHENA.MIT.EDU (Lee Collins)
Fri Mar 1 18:55:42 1996
Date: Fri, 01 Mar 1996 16:34:00 -0600
From: Lee Collins <spacely@xsite.net>
To: garya@dreamchaser.org
CC: java-interest@java.sun.com
What Gary is saying is correct.
ALL methods in Java are virtual.
However, to force the functionality that you want you may have to cast "this" inside Base.bar()
Something like:
class Base{
public bar (int x)
{
//
// do first part
//
//
// this should create a reference to an object of type Base.
// and since the class Base contains the correct foo()
//
((Base)this).foo(x);
};
}
I think this will work.
Please let me know if it doesn't.
I will try it this weekend.
Lee Collins
-------------------------------------------------------------------------------------------
>
> >> 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)
> -
--
------------------------------------------------------------------------------
class LeeCollins
extends JavaDeveloper
implements IllustriousJavaCode
{
final static String organization = "Spacely Text and Binary Goods Inc.";
final static String emailAddress = "Spacely@XSite.net";
final static String phoneNumber = "312/684-4444";
}
------------------------------------------------------------------------------
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com