[Tutor] impact of OO
alan.gauld@bt.com
alan.gauld@bt.com
Mon, 8 Apr 2002 00:13:34 +0100
> > tutor for a discussion of polymorphism and inheritance)
> I read the relevant page in your tutor. I'd like to make sure that I
> understand what it is, though.
>
> It sounds as though it's really nothing more than a way of taking
> several different classes which might be used in similar (though
> different) ways, and giving them identical method names so
> that they can be accessed without having to write
> special-circumstance code that applies specifically to that
> particular class.
Absolutely correct. It is *the* cornerstone of OOD(as opposed
to OOP!).
> object in the contents of those elements. If you did not use this
> technique, then you would have to check for the type of object and do
> something like "if shape isanobject(triangle) then... else if shape
> isanobject(square) then...".
Exactly so.
The point being that you can take a bunch of objects (shapes
in this case) and treat them generically, relying on each
object to 'know' how to respond to the messages. In practice
we do that by coding each object class with its own version
of the operation and leave the language to figure out which
one is needed. (This is *much* harder to do in statically
typed languagesc like Javaand C++ BTW)
> abiding by a certain convention in naming class methods.
Just so, in fact this has its own name in OO circles its
called defining the "common protocol" of the objects.
[ Getting more esoteric still its related to a bit of
Computer Science called the Liskoff Substitution Principle
which describes how to define abstract data types such
that they appear identical to the basic data type from
which they are descended. It is actually stricter than
normal OO inheritance/polymorphism rules. But if you are
interested there are some fairly heavy papers on the web
to read ]
> If so, then this leads directly to the concept of overriding,
Yes although overridding in languages less flexible than
Python can have wider ramifications and is slightly different
to polymorphism. C++ users sometimes call overriding static
polymorphism whereas polymorphism via inheritance is called
dynamic polymorphism. In Smalltalk and Python etc we don't
have to care or worry :-)
> that the subclass's method supplants the parent class's method.
The commonest case is probably operator overriding which is
what we do inpython when we define our own __add__() method
or __getitem__() etc. But as you say you can do it with any
method.
One thing you an't do in Python but can in some languages
is override the same method name within the same class,
like this:
class C:
def F(self, anInt): ...
def F(self, aString): C.F(self,int(aString))
def F(self, aList):map(lambda n: C.F(self,int(n)),aList)
def F(self, intA,intB,intC): C.F(self,intA+intB+intC)
This won't work min Python because its dynamically typed
so they all look the same to the interpreter. But in C++
its how you have the same method name but can pass different
typed arguments (and different numbers of arguments) to it.
We can do some of that using default params and the ret
using dynamic typechecking:
if type(p) == types.integer:
elif type(p) == types.string:
etc
> subclass its own, different, method name.
But then you can't put your new object type in a list and expect
the old list handling code to use your new class. It would
have to be modified to call the new method name - baaaaad...
HTH
Alan G