Final Method
- If you declare method as
final
in super class
it can't be overridden in any of its subclass.
Example :
<?php
class Super
{
public final function display()
{
echo "Inside the final function";
}
}
class Sub extends Super
{
function display()
{
echo "Final function";
}
}
$obj=new Sub();
s$obj->display();
?>
Output :
Fatal error: Cannot override final method Super::display()
Class as Final
- If you can declare a class as final, which will intercept
anyone from extending it.
Example :
<?php
final class Super
{
public function display()
{
echo "Inside the final function";
}
}
class Sub extends Super
{
function display()
{
echo "Inside the final function";
}
}
$obj=new Sub();
$obj->display();
?>
Output :
Fatal error: Class Sub may not inherit from final class (Super)