Home >> PHP >> What is overriding in PHP

What is overriding in PHP

Overriding:-Overriding required when your parent class have some method,but in your child class you want the same method with different behavior by overriding of method you can complete change its behaviour from parent class to implement method  overriding in opps we commonly create same method in child class.
Example:-

class testParent{
      public function f1(){
            echo 1;
      }
      public function f2(){
             echo 2;
      }
}
class testChild extends testParent{
      public function f2($a){
             echo $a;
      }
}
$obj=new testChild();
$obj->f2("ankur");

Output:-
ankur

Example:-

class parentClass{
      function name(){
            return 'Parent';
      }
}
class childClass extends parentClass{
      function name(){
            return 'Child';
      }
}
$obj=new childClass();
$obj->name();

Output:-
Child

Post Your Comment

Next Questions
What is abstraction
What is abstract class
Implementation of abstract method
What is interface
What is difference between abstract class and interface
What is inheritance
Inheritance types
What is single inheritance
What is multiple inheritance
What is multilevel inheritance
How to use static method and property in inheritance
How to use self and parent with static
How to use self and parent without static
What is visibility
What is the difference between public, private, and protected
What is final
What is final class
What is final method
What is final variable
How to print half pyramid using star
How to print half pyramid using number
How to print inverted half pyramid using star
How to print inverted half pyramid using number
How to print half pyramid using one to ten number
How to print pyramid pattern using star

Copyright ©2022 coderraj.com. All Rights Reserved.