Method Overriding

In method overriding, we are declaring the same method in child classes as in the parent class with different behavior. The use of method overriding is generally in the case when we do not want to inherit the method behavior in the child class. So we can redeclare that method in a child class with different behavior. This process is called method overriding.

Syntax of Method Overloading

<?php
    Class FirstClass {
        function welcome($name){
             echo 'Welcome '.$name;
        }
        function good($timing){
             echo 'GOOD '.$timing;
        }
    }
    Class SecondClass extends FirstClass {
        function welcome($name, $city) {
            echo 'Welcome '.$name.' at '.$city.'.';
        }
    }
    $obj = new SecondClass();
    $obj->good('Morning!');
    echo '<br/>';
    $obj->welcome('John', 'New Delhi');
?>

Output

GOOD Morning!
Welcome John at New Delhi.






Read more articles


General Knowledge



Learn Popular Language