Method Visibility & Access Modifiers
Master Ruby's access modifiers: public, private, and protected methods with practical examples and best practices.
🚀 Interactive Practice
Interactive Code Runner
Ruby Code Editor
🎯 Key Takeaways
- public: Default visibility, accessible from anywhere
- private: Only callable within the same object
- protected: Accessible by same class and subclasses
- Encapsulation: Hide internal implementation details
Method Visibility & Access Modifiers
Method visibility controls how methods can be accessed in Ruby. Understanding public, private, and protected methods is essential for proper object-oriented design and encapsulation.
Understanding Method Visibility
Method Visibility: Controls who can call a method. Ruby has three levels: public (default), private, and protected.
Basic Example
- Public: Can be called from anywhere (default)
- Private: Can only be called from within the same object
- Protected: Can be called from the same class or subclasses
Public Methods
Public Methods: The default visibility. Can be called from anywhere - outside the class, from subclasses, or from within the class itself.
Public Method Examples
Key Points:
- Public is the default visibility for all methods
- Can be called from anywhere - outside the class, subclasses, or within the class
- Use
publickeyword to explicitly declare (optional) - Most interface methods should be public
Private Methods
Private Methods: Can only be called from within the same object. Cannot be called with an explicit receiver (no dot notation from outside).
Private Method Examples
Key Points:
- Cannot be called with explicit receiver (no
object.private_method) - Can only be called from within the same object
- Use for internal implementation details and helper methods
- All methods after
privatekeyword become private
Protected Methods
Protected Methods: Can be called from the same class or its subclasses. Cannot be called from outside the class hierarchy.
Protected Method Examples
Key Points:
- Can be called from the same class or its subclasses
- Cannot be called from outside the class hierarchy
- Useful for methods that subclasses need but shouldn't be public
- Less commonly used than public and private
Advanced Visibility Patterns
Advanced Patterns: Ruby provides flexible ways to control method visibility including selective visibility changes, module inclusion, and dynamic visibility control.
Selective Visibility Changes
Module Inclusion & Visibility
Interactive Practice: Method Visibility
Practice Exercise
Challenge: Implement the BankAccount class with proper method visibility. Make validation methods private and comparison methods protected.
Best Practices & Common Pitfalls
✅ Best Practices
- Keep the public interface minimal and focused
- Use private for internal implementation details
- Use protected sparingly - only when subclasses need access
- Group methods by visibility for better readability
- Document public methods thoroughly
- Use descriptive names for private methods
❌ Common Pitfalls
- Making everything public (breaks encapsulation)
- Using protected when private would suffice
- Forgetting that private methods can't use explicit receivers
- Not grouping methods by visibility
- Making internal methods public for testing
- Overusing private methods (can make code hard to test)
Method Visibility Mastery Summary
You've Mastered Method Visibility!
Public Methods
Default visibility, accessible from anywhere
Private Methods
Internal implementation, same object only
Protected Methods
Class hierarchy access, subclasses included
Method visibility is essential for proper object-oriented design. Use it to create clean, maintainable interfaces while protecting internal implementation details.