in reply to Changing Signature of overrided methods

Methods have no signatures, and subroutines and methods have no defined return type either.

Maybe you can show us some relevant code and explain the problems you encounter in a more concrete way?

  • Comment on Re: Changing Signature of overrided methods

Replies are listed 'Best First'.
Re^2: Changing Signature of overrided methods
by dvinay (Acolyte) on Apr 12, 2015 at 12:48 UTC
    Thanks Corion, please find the sample code snippet
    package Person; sub new { my $class = shift; my $self = { _firstName => shift, _lastName => shift, _ssn => shift, }; # Print all the values just for clarification. print "First Name is $self->{_firstName}\n"; print "Last Name is $self->{_lastName}\n"; print "SSN is $self->{_ssn}\n"; bless $self, $class; return $self; } sub setFirstName { my ( $self, $firstName ) = @_; $self->{_firstName} = $firstName if defined($firstName); return $self->{_firstName}; } sub getFirstName { my( $self ) = @_; return $self->{_firstName}; } 1; *************************************************** package Employee; use Person; use strict; our @ISA = qw(Person); # inherits from Person # Override constructor sub new { my ($class) = @_; # Call the constructor of the parent class, Person. my $self = $class->SUPER::new( $_[1], $_[2], $_[3] ); # Add few more attributes $self->{_id} = undef; $self->{_title} = undef; bless $self, $class; return $self; } # Override helper function sub getFirstName { my( $self ) = @_; # This is child class function. print "This is child class helper function\n"; return 0; }

    In above case, overrided method getFirstName() returns "$self->{_firstName}" in base class, but i want to return boolean like 1 or 0 in the derived class is that possible..

      Hello dvinay,

      Of course it’s possible! What happened when you tried it?

      But, just because something is possible, that doesn’t mean it is a good idea. And in this case, it’s not, because it undermines the whole point of inheritance. By making Employee inherit from Person, you are saying that an Employee ISA Person; or, in other words, that everything that is true of a Person is also true of an Employee. In particular, this means that you can call the getFirstName method on an Employee and expect to get back the first name of a Person.

      One place this comes into its own is when you have other subclasses of Person — say, Manager and Customer — and you want to process them all together in a Person array:

      my @People; ... push @People, Employee->new(...); ... push @People, Customer->new(...); ... push @People, Manager->new(...); ... # etc. ... for my $person (@People) { ... print $person->getFirstName; ... }

      (This is called polymorphism.) There is nothing wrong with overriding an inherited method — that’s a major feature of the whole OO paradigm — but changing the method’s signature or return type undermines polymorphism and so negates one of the main values of using inheritance.

      You don’t say why you want to do this, but whatever the reason, it’s almost certainly a symptom of a faulty OO design.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        Thanks a lot Athanasius.. Actually i am struck in case where i need to override a base class method either by changing the number of input parameters to the method or return type.. I agree with you changing one of above defeats the purpose of OOPs..