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);

?>




Function    Overloading   using    function __call :

<?Php

 

Class Test12

 

{

    Public Function __call($Name, $Arguments)

               

    {

        If ($Name === 'Test'){

            If(Count($Arguments) === 1 ){

                Return $This->Test1($Arguments[0]);

            }

            If(Count($Arguments) === 2){

                Return $This->Test2($Arguments[0], $Arguments[1]);

            }

                                                If(Count($Arguments) === 3){

                Return $This->Test3($Arguments[0], $Arguments[1],$Arguments[2]);

            }

 

        }

    }

 

    Private Function Test1($Data1)

    {

       Echo $Data1;

    }

 

    Private Function Test2($Data1,$Data2)

    {

       Echo $Data1.' '.$Data2;

    }

                 Private Function Test3($Data1,$Data2,$Data3)

    {

       Echo $Data1.' '.$Data2.$Data3;

    }

}

 

$Test = New Test12();

 

$Test->Test('One Argument');                //Echoes "One Argument"

 

$Test->Test('Two','Arguments');

 

$Test->Test('Three','Om','Test');          //Echoes "Two Arguments"

 

 

?>

 

Comments

Popular posts from this blog

Query to get concurrent Program details with Parameter list and value set attached to parameter.

Japan Style: Architecture Interiors Design

AR and OM link in oracle apps / AR transaction and Sales Order join query