Challenge - 5 Problems
Multiple Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of multiple interface implementation
What is the output of this PHP code that implements two interfaces in one class?
PHP
<?php interface A { public function foo(); } interface B { public function bar(); } class C implements A, B { public function foo() { echo "foo called\n"; } public function bar() { echo "bar called\n"; } } $obj = new C(); $obj->foo(); $obj->bar(); ?>
Attempts:
2 left
💡 Hint
Remember that a class implementing multiple interfaces must define all their methods.
✗ Incorrect
Class C implements both interfaces A and B, so it must define both foo() and bar(). The code calls both methods, so both outputs appear.
❓ Predict Output
intermediate2:00remaining
Which method is called from which interface?
Given this PHP code with two interfaces and a class implementing both, what is the output?
PHP
<?php interface X { public function test(); } interface Y { public function test(); } class Z implements X, Y { public function test() { echo "test method from Z\n"; } } $z = new Z(); $z->test(); ?>
Attempts:
2 left
💡 Hint
One method can satisfy multiple interfaces if signatures match.
✗ Incorrect
Both interfaces declare the same method name and signature. The class implements one method that satisfies both interfaces. Calling test() outputs the defined string.
❓ Predict Output
advanced2:00remaining
Output when interface methods have different signatures
What happens when a class tries to implement two interfaces with methods of the same name but different parameters?
PHP
<?php interface I1 { public function run(int $a); } interface I2 { public function run(string $b); } class Test implements I1, I2 { public function run($param) { echo gettype($param) . "\n"; } } $test = new Test(); $test->run(5); $test->run("hello"); ?>
Attempts:
2 left
💡 Hint
PHP requires method signatures to be compatible when implementing multiple interfaces.
✗ Incorrect
The class method run() cannot satisfy both interface signatures because one expects int and the other string. This causes a fatal error.
❓ Predict Output
advanced2:00remaining
Output of interface constants in multiple interfaces
What is the output of this PHP code using constants from multiple interfaces implemented by one class?
PHP
<?php interface First { const VALUE = 10; } interface Second { const VALUE = 20; } class Demo implements First, Second { public function show() { echo self::VALUE . "\n"; } } $obj = new Demo(); $obj->show(); ?>
Attempts:
2 left
💡 Hint
When interfaces have constants with the same name, the class must resolve ambiguity.
✗ Incorrect
The class Demo inherits VALUE constant from both interfaces. Using self::VALUE is ambiguous and causes a fatal error.
🧠 Conceptual
expert2:00remaining
Why must a class implement all interface methods?
In PHP, why does a class that implements multiple interfaces have to define all methods declared in those interfaces?
Attempts:
2 left
💡 Hint
Think about what interfaces represent in object-oriented programming.
✗ Incorrect
Interfaces specify methods that a class promises to implement. This contract ensures consistent behavior and allows polymorphism.