Method overriding means a child class changes the method of the parent class but keeps the same method name.
In method overriding, the child class replaces the parent class method with its own code.
Example:
<?php
class Animal {
public function sound(){
echo "Animal makes sound";
}
}
class Dog extends Animal {
public function sound(){
echo "Dog barks";
}
}
$obj = new Dog();
$obj->sound();
?>- Animal is the parent class.
- Dog is the child class.
- Both classes have the same method sound().
- The child class overrides the parent method.