Shawn McKinley wrote:

> Hello all,
>   I am wondering if you can have object inherited between
> packages when the child packages have their own object
> creation without explicitly setting the parent object in
> the child?  Is there a way to inherit the parent object?
> Example below (sorry for the length).
>
> TIA,
> Shawn

Hi Shaun,

What is it you are trying to model?  Generally, it is not a good
idea to make objects interdependent more than absolutley
necessary.  I'm a little unclear about what you are getting at.  It
isn't a good idea to try to access data members of a parent object,
because there should not be a separate parent in inheritance
relationships.  Say you have a class Building

package Building;

use strict;
...

sub initialize {
   my $self = shift;
   $self->{'footprint width'} = shift;
   $self->{'footprint length'}= shift;
   ... other generic building stuff
}

sub footprint_width {
   my $self = shift;

   return $self->{'footprint width'};
}

...and you decide to subclass House from building.  You have access
to the methods of the parent class, so you can call them without
having to redefine them.  On the other hand, you should not think
of the data as being part of any other object.  The relationship is
an "izza" relationship.  The House "is a" Building.  There is no
separate Building to have the House's 'footprint width' member.

package House;

use strict;
...

use Building;

use Exporter;

my @ISA = 'Building';

sub new {
   my $class = shift;
   ...yada, yada
   $self->initialize(@_);
}

sub initialize {
   my $self = shift;

   $self->{'footprint width'} = shift; # just as with the Building
initializer
   ...
}

Note that this data value is assigned to the object of the
immediate class without reference to the parent class.  The thing
is, the footprint_width method of the parent can be called on this
object, and will get the member of the House object.

Inheritance is basically about inheriting those metohds.  Use it
sparingly, and aim for transparency.

Joseph


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to