Learn PHP Tutorial

PHP OOP – Methods Overriding


Overriding refers to the ability of a subclass to re-implement a method inherited from a superclass.

In function overriding, both parent and child class should have same function name with the same parameter. It is basically used in the purpose to replace parent method in the class or change the behavior of parent class method.

  • Only inherited methods can be overridden.
  • Final and static method can not be overridden.
  • The overriding method must have the same parameter.

Syntax

<?php
class Father
{
    function display()
    {
        echo "Super Class.";
    }
}

class Son extends Father{
    function display()
    {
        echo "Son Class.";
    }
}

class Daughter extends Father
{
    function display()
    {
        echo "Daughter Class";
    }
}
?>