Method overriding in PHP is when a child class defines a method with the same name as a method in its parent class, replacing the parent's method behavior.
Click to reveal answer
beginner
Why do we use method overriding?
We use method overriding to change or extend the behavior of a method inherited from a parent class, allowing child classes to customize functionality.
Click to reveal answer
intermediate
How do you call the parent class method inside an overridden method in PHP?
You use <code>parent::methodName()</code> inside the child class method to call the parent class's version of the method.
Click to reveal answer
beginner
What happens if the child class does not override a method from the parent class?
If the child class does not override the method, it inherits and uses the parent's method as is.
Click to reveal answer
intermediate
Can private methods be overridden in PHP?
No, private methods cannot be overridden because they are not accessible to child classes.
Click to reveal answer
What keyword is used to call the parent class method inside an overridden method in PHP?
Aparent::
Bsuper::
Cthis::
Dbase::
✗ Incorrect
In PHP, parent::methodName() calls the parent class's method.
If a child class overrides a method, which method is executed when called on the child object?
AChild class method
BParent class method
CBoth methods
DNone
✗ Incorrect
The child class method overrides the parent method and is executed.
Can a private method in a parent class be overridden by a child class in PHP?
AYes, always
BOnly if declared final
CNo, private methods are not accessible
DOnly if static
✗ Incorrect
Private methods are not visible to child classes, so they cannot be overridden.
What happens if a child class does not override a method from its parent?
AError occurs
BParent method is used
CMethod is ignored
DChild class must define it
✗ Incorrect
The child class inherits and uses the parent's method if it does not override it.
Which of these is a reason to override a method?
ATo avoid inheritance
BTo delete the parent method
CTo make the method private
DTo change inherited method behavior
✗ Incorrect
Overriding lets you change or extend the behavior of an inherited method.
Explain method overriding in PHP and how it helps in object-oriented programming.
Think about how child classes can change what they inherit.
You got /4 concepts.
Describe how to call a parent class method from an overridden method in PHP and why you might want to do that.
Sometimes you want to add to the parent's method, not just replace it.
You got /3 concepts.
Practice
(1/5)
1. What does method overriding allow a child class to do in PHP?
easy
A. Change the behavior of a method inherited from the parent class
B. Create a new method with a different name
C. Prevent the parent class from being instantiated
D. Automatically call all parent methods
Solution
Step 1: Understand method overriding concept
Method overriding means a child class uses the same method name as the parent to change its behavior.
Step 2: Identify what overriding does
It replaces or modifies the parent's method behavior in the child class.
Final Answer:
Change the behavior of a method inherited from the parent class -> Option A
2. Which of the following is the correct syntax to override a method named greet in a child class in PHP?
easy
A. function greetChild() { /* new code */ }
B. function greet() { /* new code */ }
C. override function greet() { /* new code */ }
D. parent::greet() { /* new code */ }
Solution
Step 1: Recall method overriding syntax
To override, define a method with the same name in the child class.
Step 2: Check options for correct syntax
Only function greet() { /* new code */ } defines function greet() correctly without extra keywords.
Final Answer:
function greet() { /* new code */ } -> Option B
Quick Check:
Same method name in child = override [OK]
Hint: Use same method name in child class to override [OK]
Common Mistakes:
Adding 'override' keyword which PHP does not use
Changing method name to something else
Trying to call parent method syntax incorrectly
3. What will be the output of this PHP code?
class ParentClass {
function sayHello() {
echo "Hello from Parent";
}
}
class ChildClass extends ParentClass {
function sayHello() {
echo "Hello from Child";
}
}
$obj = new ChildClass();
$obj->sayHello();
medium
A. Hello from ParentChild
B. Hello from Parent
C. Hello from Child
D. Error: Method not found
Solution
Step 1: Identify method overriding in ChildClass
ChildClass defines sayHello() which overrides ParentClass method.
Step 2: Check which method is called
Creating ChildClass object calls ChildClass's sayHello(), printing "Hello from Child".
Final Answer:
Hello from Child -> Option C
Quick Check:
Child method overrides parent = "Hello from Child" [OK]
Hint: Child method runs if overridden, not parent [OK]
Common Mistakes:
Expecting parent method output instead of child
Thinking both outputs print together
Assuming error due to method name clash
4. Find the error in this PHP code that tries to override a method:
class Base {
function show() {
echo "Base";
}
}
class Derived extends Base {
function show() {
parent.show();
echo " Derived";
}
}
$obj = new Derived();
$obj->show();
medium
A. Class Derived must declare show() as public
B. Cannot override methods in PHP
C. Missing return statement in show()
D. Incorrect syntax to call parent method; should use parent::show()
Solution
Step 1: Identify how parent method is called
The code uses parent.show() which is invalid syntax in PHP.
Step 2: Correct syntax for calling parent method
PHP requires parent::show() to call the parent's method inside child.
Final Answer:
Incorrect syntax to call parent method; should use parent::show() -> Option D
Quick Check:
Call parent method with parent::method() [OK]
Hint: Use parent::method() to call parent method inside child [OK]
Common Mistakes:
Using dot (.) instead of :: to call parent method
Thinking overriding is not allowed
Forgetting to call parent method when needed
5. Given these classes, what will be the output?
class Animal {
function sound() {
echo "Some sound";
}
}
class Dog extends Animal {
function sound() {
parent::sound();
echo " and Bark";
}
}
class Puppy extends Dog {
function sound() {
echo "Yip";
}
}
$pet = new Puppy();
$pet->sound();
hard
A. Yip
B. Some sound and Bark and Yip
C. Some sound and Bark
D. Error: Cannot override multiple times
Solution
Step 1: Trace method calls for Puppy object
Puppy overrides sound() and only echoes "Yip" without calling parent.
Step 2: Understand overriding chain
Since Puppy method does not call parent::sound(), Dog and Animal methods are not executed.
Final Answer:
Yip -> Option A
Quick Check:
Child override without parent call = only child output [OK]
Hint: If child method skips parent call, only child output shows [OK]
Common Mistakes:
Assuming all parent methods run automatically
Expecting combined outputs without explicit parent calls
Thinking multiple overrides cause errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.