Page 1 of 1

Do child class overriding meathods ned 2 b declared proteted

Posted: Sat Dec 20, 2014 12:09 pm
by gautamz07
do child classes extending a parent class for example have a method of the parent class being over-ridden , does that class need to be declared protected or private ??

eg.

method in parent class:

Code: Select all

class person {
                protected function set_name($new_name) {
                        if ($new_name != "Jimmy Two Guns") {
                                $this->name = strtoupper($new_name);
                        }
                }
        } 
method override in child class :

Code: Select all

class employee extends person {
                protected function set_name($new_name) {
                        if ($new_name == "Stefan Sucks") {
                                $this->name = $new_name;
                        }
                }
        } 
is the protected necessary ???

Re: Do child class overriding meathods ned 2 b declared prot

Posted: Sat Dec 20, 2014 3:41 pm
by requinix
It has to be the same as the parent's or more accessible, so protected or public. If you leave off the "protected" then it will be public.

Re: Do child class overriding meathods ned 2 b declared prot

Posted: Sun Dec 21, 2014 11:48 pm
by gautamz07
Thanks