Bird
0
0

You want to create a base class Document that cannot be extended, but you want to allow extending a method print() in child classes. Which is the correct approach?

hard📝 Application Q15 of 15
PHP - Inheritance and Polymorphism
You want to create a base class Document that cannot be extended, but you want to allow extending a method print() in child classes. Which is the correct approach?
ADo not declare class Document as final, and declare method print() normally
BDeclare class Document as final, and method print() normally (not final)
CDo not declare class Document as final, but declare method print() as final
DDeclare class Document as final, and method print() as final
Step-by-Step Solution
Solution:
  1. Step 1: Understand final class effect

    Declaring a class as final prevents any extension, so no child classes can exist to override methods.
  2. Step 2: Understand method overriding

    To allow overriding print(), the class must be extendable and the method must not be final.
  3. Step 3: Choose correct option

    Do not declare class Document as final, and declare method print() normally allows extending the class and overriding the method. Options B and D prevent extension. Do not declare class Document as final, but declare method print() as final prevents method overriding.
  4. Final Answer:

    Do not declare class Document as final, and declare method print() normally -> Option A
  5. Quick Check:

    Allow extension and method override = no final on class or method [OK]
Quick Trick: Final class blocks extension; omit final to allow overrides [OK]
Common Mistakes:
  • Declaring class final but expecting method override
  • Declaring method final when override is needed
  • Confusing final class and final method effects

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes