Bird
0
0

How can you access a constant defined in a parent class from a subclass in Ruby?

hard📝 Application Q9 of 15
Ruby - Class Methods and Variables
How can you access a constant defined in a parent class from a subclass in Ruby?
AConstants are not inherited; you must redefine them
BUse 'super' keyword to access constants
CUse instance variable syntax @CONSTANT
DUse the scope resolution operator with the parent class name
Step-by-Step Solution
Solution:
  1. Step 1: Understand constant inheritance in Ruby

    Constants are inherited by subclasses but can be accessed explicitly using parent class name.
  2. Step 2: Access constant using scope resolution operator

    Use ParentClass::CONSTANT to access parent's constant inside subclass.
  3. Final Answer:

    Use the scope resolution operator with the parent class name -> Option D
  4. Quick Check:

    Parent constant access = ParentClass::CONSTANT [OK]
Quick Trick: Use ParentClass::CONSTANT to access parent's constant [OK]
Common Mistakes:
  • Thinking constants are not inherited
  • Using instance variable syntax for constants
  • Trying to use 'super' for constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes