[Tutor] Inheritance help

Alan Gauld alan.gauld at btinternet.com
Thu Jul 3 03:05:15 CEST 2008


"sean_mcdaniel" <sean.m.mcdaniel at gmail.com> wrote

> I am having problems with classes and subclasses. Do the different 
> __init__
> statements have to have different argument lists?

You got the solution to your specific question but to answer
your more pholosophical one:

sub classes should ideally have the same signature as their
superclass (the Liskov Substitution Principle or LSP). In other
words you should be able to plug a sub class in anywhere that the
superclass can be used.

This implies that depending on language provisions) your
sub class should have

a) one constructor with the same signature as the superclass
(if multiple constructors are supported).
b) a constructor with the same parameters as the superclass plus
some more - provided the new parameters can have default values
(the Python option)
c) a constructor with the same parameters as the superclass,
plus an initialisation method with any extra parameters which clients
must call immediately after construction if variant behaviour is 
required.
(where only single constructor is supported and no default params)

So not only is it possible but its a good idea from a pure OOP 
viewpoint.
Of course there are plenty examples of subclasses which do not follow
the LSP but they are limited in reuse capability as a result.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list