[Tutor] Calling method in parent class

Alan Gauld alan.gauld at btinternet.com
Tue May 12 18:43:24 CEST 2009


"spir" <denis.spir at free.fr> wrote


>> OK, bad example. But assume I have the same method in both classes and
>> want to call the method in the parent.
> 
> That should not happen! Basic contract is: same name = same meaning.

Nope, its called polymorphism.

The semantics may be the same but the implementation detail may differ 
and more specifically  the child implementation is likely to be a slight 
change on the parent. Its the calling of the parent code that keeps the 
essential semantics consistent.


> Either you implement a method in a parent class to let all instances 
> of child classes use it; 

Thats what he is doing, except that in the overriding metjod he wants 
to reuse the inherited functionality.

> Having two methods with the name that both need two be used 
> on the same object is clearly a design flaw. What do you think?

Two methods only one message. It is what polymorphism is all about.

> The only case is when the parent method performs a part of 
> what child class methods have to do. 

Exactly and in my experience of OO inheritance thats what happens 
most of the time.

Think:

foo.draw(), save(), write(), print(), copy(), read(), move(), etc

All of these operations will likely use the superclass functionality
and add a little bit of local processing for any attributes in the 
child class.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list