in reply to Re: OOP and derived classes
in thread OOP and derived classes

>Here's a simple answer: yes, you should. You don't get 'base >instance' automatically.

but it's looks like its optional in Perl? what happens when the base class is not instantiated and I call a base class method which accesses some base class properties ,through a derived object ? will the method be called as a base class method or will it be called as a derived one (the first parameter sent to the method would be a base object or a derived one?

Replies are listed 'Best First'.
Re^3: OOP and derived classes
by Anonymous Monk on Feb 14, 2015 at 20:17 UTC
    but it's looks like its optional in Perl?
    Yes.
    what happens when the base class is not instantiated and I call a base class method which accesses some base class properties ,through a derived object ?
    If, say, the object in question is a hash, then base class will find undef instead of whatever it wanted to find, like with all other hashes.
    will the method be called as a base class method or will it be called as a derived one (the first parameter sent to the method would be a base object or a derived one?
    The base class will get the whole thing, 100% of it. So, in Java's terms, the base method will get the derived object.
      So can I call a derived method with a base object,like can I use a base object in place of a derived object,for example when a method expects the derived class but I do pass the base class instead?
        If a method isn't found AUTOLOAD is called, if provided.

        > If you call a method that doesn't exist in a class, Perl will throw an error. However, if that class or any of its parent classes defines an AUTOLOAD method, that AUTOLOAD method is called instead.

        Please instead of torturing us with Java speak try out some Perl code and post snippets.

        update

        This $obj->meth() is only evaluated at runtime and "meth" is just a string used to resolve the corresponding sub in the possible packages.

        IoW you can try to call everything everywhere without compilation error.

        you can even do

        $str="meth"; $obj->$str();

        update

        DB<104> sub meth { $self=shift @_; print "calling ",$self->{name},"->meth(@_)"; } DB<106> $obj = bless {name=>"obj"}, main; => bless({ name => "obj" }, "main") DB<107> $str="meth" => "meth" DB<108> $obj->$str("bla") calling obj->meth(bla)

        Update

        Also keep in mind that a package is basically a hash which can be altered anytime, i.e. subs can be dynamically added or deleted at runtime.

        Cheers Rolf

        PS: Je suis Charlie!

        Yes you can, but you'll have to initialize the base object yourself (if it needs initialisation).

        Use of Inheritance creates tight coupling and loss of encapsulation though, so if you're writing new code, then using delegation (or Roles) would be safer.

        Is that possible in Java? What happens when the method needs to access an attribute that doesn't exist on the base class?
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ