sugarboy has asked for the wisdom of the Perl Monks concerning the following question:

Hi All
Is it possible to call a Child class Member functions using
Parent Class Object.
Thanks,
Sagar
  • Comment on Can we call a Child Class Member functions using Parent Class Object

Replies are listed 'Best First'.
Re: Can we call a Child Class Member functions using Parent Class Object
by jethro (Monsignor) on Jun 07, 2011 at 11:47 UTC

    Yes, but you shouldn't.

    To call any function in a class aka package (basically a class is a package) you write class::function($yourobject, ...). You need to add your object as the first parameter if the function is a method, because methods expect this.

    You shouldn't do this because usually child classes add functionality that the parent class doesn't have. Later on you might change the child class and for example add a variable that this function you want to call needs. Bamm, your program doesn't work anymore because the parent object doesn't have such a variable.

    If your child class function is valid for the parent as well, why don't you move the function to the parent?

      You need to add your object as the first parameter if the function is a method, because methods expect this.

      It's not a method if you call it that way.

Re: Can we call a Child Class Member functions using Parent Class Object
by Corion (Patriarch) on Jun 07, 2011 at 11:39 UTC

    What happened when you tried it?

    If you want an answer more specific to your problem, please help us to help you better. Please do post a small, self-contained sample program (about 10 to 20 lines maximum) and post the input data you give it and the output you get. Please also describe how the output you get differs from the output you want.