Re: Behavior of OO
Mike Schilling wrote:
Philipp wrote:
Hello
I have a somewhat naive question about OO.
If you have two classes Fruit and Apple extends Fruit. You declare:
Fruit f = new Fruit();
Fruit a = new Apple();
f.eat();
a.eat();
if you call the method .eat() on f, you get the eat() from Fruit and
if you call eat() on a, you get the overriden eat() behavior in Apple.
On the other hand, if somewhere else, the following two methods are
defined:
public void makeJam(Fruit f){};
public void makeJam(Apple a){};
and you call makeJam(f) and makeJam(a), both times it's the
makeJam(Fruit) which is called.
So in one case, the runtime "knows" that the object is actually an
Apple although it was declared a Fruit, and makes a distinction by
calling the correct method. In the other case it does not make the
distinction. (yes I know I could check with instanceof and cast)
Why is this so? Is this a design choice in the language? Or is it for
performance reasons? Are there cases when having it the other way (ie.
chosing the method signature which best fits the arguments) would lead
to UB?
Java inherits this behavior from C++. Stroustrup explains why C++ does this
in his _The Design and Evolution of C++_, the answer being that trying to
dispatch on multiple types (the type of "this" plus the types of one or more
arguments) is far ore complicated and difficult to do efficiently than
dispatching based on just one type. It can also leads to problems like the
folllowing:
Suppose there are three methods foo(Object, Object), foo(Object, String),
and foo(String, Object). Which should be called, given
Object o1 = "abc";
Object o2 -= "def";
foo(o1, o2);
There was a thread about this just recently on comp.object
Often times the "workaround" in languages that don't dispatch that way
is to use the Visitor pattern. That way, the object itself is
responsible for the final version of the method invoked, so the
polymorphic behavior is preserved.
In general, I think it is often better to have the Apple handling any
difference in makeJam that might happen simply because the Fruit is an
Apple. Sometimes that solution isn't feasible, but its been my
experience that it /often/ is.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>