inheritance

Darren Duncan darren at DarrenDuncan.net
Thu Feb 20 20:17:58 CST 2003


On Wed, 19 Feb 2003, nkuipers wrote:
> Child constructor is as follows:
>
> sub new {
> 	my $type = shift;
> 	my $self = {};
> 	$self->{bogus_key} = BIO::Basic->new(@_);
> 	bless $self, $type;
> }

Your child constructor is incorrect, and in this case, unnecessary.
Constructors are inherited, so when your child module starts with "use
base 'Bio::Basic'", Perl see's all of that class' methods in Bio::Annotate
without you doing anything else in the latter.  This means that Perl would
call the new() of Bio::Basic unless you declared a new() in the child.
Since the parent class uses the value in $class in bless(), as any normal
Perl class should (any class should be inheritable), it will already do
the right thing, since $class would contain Bio::Annotate when you say
"new Bio::Annotate", and the parent constructor will bless into
Bio::Annotate.  The only reason to make your own new() is if you need to
override or supplement functionality in the parent's new().  In the latter
case, your child class' new() should look like this:

sub new {
	my $self = SUPER::new(@_);  # puts blessed hash in $self
	# put other special functionality here
	return($self);
}

Note that "SUPER" is a special Perl keyword that refers to the parent
class.  That said (and I don't really like this), when you have multiple
inheritence, SUPER won't work and you have to say instead
"Bio::Basic::new(@_)".

-- Darren Duncan




More information about the Victoria-pm mailing list