Bird
0
0

In Ruby, which of the following best describes a protected method?

easy📝 Conceptual Q1 of 15
Ruby - Inheritance
In Ruby, which of the following best describes a protected method?
AIt can be called from anywhere, like a public method.
BIt can be called only within the defining class, not even by subclasses.
CIt can be called only within the defining class and its subclasses, including instances of the same class.
DIt can be called only by the object itself, not by other instances.
Step-by-Step Solution
Solution:
  1. Step 1: Understand protected method scope

    Protected methods in Ruby can be called by any instance of the defining class or its subclasses, but not from outside.
  2. Step 2: Compare with other visibility types

    Unlike private methods, protected methods allow calls between instances of the same class or subclasses.
  3. Final Answer:

    It can be called only within the defining class and its subclasses, including instances of the same class. -> Option C
  4. Quick Check:

    Protected method scope = It can be called only within the defining class and its subclasses, including instances of the same class. [OK]
Quick Trick: Protected allows calls within class and subclasses instances [OK]
Common Mistakes:
  • Confusing protected with private visibility
  • Thinking protected methods are public
  • Assuming protected methods are accessible outside the class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes