Friday, 6 November 2015

FUNCTION OVERRIDING-FUNCTION OVERLOADING

22:32


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"

 

 

?>

 



Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.

0 comments:

Post a Comment

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai

 

© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top