FUNCTION OVERRIDING-FUNCTION OVERLOADING
Function Overloading :
In Method Overloading, Methods Of The Same Class Shares The Same Name But Each Method Must Have Different Number Of Parameters Or Parameters Having Different Types And Order.
Method Overloading Means More Than One Method Shares The Same Name In The Class But Having Different Signature.
Method Overloading Is To “Add” Or “Extend” More To Method’s Behavior.
Overloading And Overriding Is A Kind Of Polymorphism. Polymorphism Means “One Name, Many Forms”.
It May Or May Not Need Inheritance In Method Overloading.
In Method Overloading, Methods Must Have Different Signature.
Function Overriding:
In Method Overriding, Sub Class Have The Same Method With Same Name And Exactly The Same Number And Type Of Parameters And Same Return Type As A Super Class.
Method Overriding Means Method Of Base Class Is Re-Defined In The Derived Class Having Same Signature.
Method Overriding Is To “Change” Existing Behavior Of Method.
Overloading And Overriding Is A Kind Of Polymorphism. Polymorphism Means “One Name, Many Forms”.
It Always Requires Inheritance In Method Overriding.
In Method Overriding, Methods Must Have Different Signature.
Function Overriding :
Function definitions in child classes override definitions with the same name in parent classes. In a child class, we can modify the definition of a function inherited from parent class.
<?php
class abc
{
var $a;
var $b;
function cal($p,$q)
{
$this->a=$p;
$this->b=$q;
$sum=$this->a+$this->b;
echo"it is addition of two parameters".$sum;
}
}
class xyz extends abc
{
var $a;
var $b;
var $c;
var $d;
function cal($p,$q)
{
$this->a=$p;
$this->b=$q;
$sum=$this->a*$this->b;
echo"multiplication of two parameters".$sum;
}
}
$obj=new xyz();
$obj->cal(5,2);
?>
Comments
Post a Comment