in reply to OO - problem with inheritance
This is definitely a hangover from Java, but whenever I do inheritance in Perl, I write the code so that the parent constructor is called from the child class' constructor. In Java, every time you extend a class, whether you say so explicitly or not, you instantiate everything all the way up the hierarchy up to java.lang.Object.
In Perl I often see people use inheritance with methods only; that is, they inherit the behavior but not the data. To me, both are necessary for object oriented inheritance.
My suggestion is to write the general constructor in the base class with a default value for the parameter checking. If a subclass needs something different, it calls the base class constructor with the proper parameters. In Java, this is super(params), in Perl the child constructor would say:
my ($class, $whatever) = @_; my %params = (); # new params to use instead of parent's params my $this = new base_class(%params); # create a new parent bless($this, $class); # rebless into the current package
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: super(params)
by IlyaM (Parson) on Jan 15, 2002 at 19:12 UTC | |
by frag (Hermit) on Jan 15, 2002 at 23:43 UTC | |
by djantzen (Priest) on Jan 16, 2002 at 07:54 UTC | |
Re: super(params)
by frag (Hermit) on Jan 15, 2002 at 23:19 UTC |