Method visibility mô tả API boundary của object.
Public
Public method gọi với explicit receiver:
1 | account.balance |
Đây là contract bên ngoài.
Private
Private method chỉ gọi không có explicit receiver (ngoại lệ syntax như setter có nuance riêng):
1 | class Account |
Private không phải security boundary. send, reflection và reopen class vẫn truy cập được. Nó là tín hiệu thiết kế: caller không nên phụ thuộc method này.
Protected
Protected method có thể gọi với explicit receiver khi receiver phù hợp trong hierarchy/context:
1 | class Account |
Hữu ích cho object cùng loại cần cộng tác trên internal state, nhưng ít dùng hơn private.
Class methods
Visibility của instance method và singleton method khác nhau:
1 | class Token |
private_class_method cũng có thể dùng.
Chọn visibility
- Public: stable behavior cho caller.
- Private: implementation detail.
- Protected: collaboration nội bộ giữa related objects.
Giảm public surface giúp refactor dễ hơn. Mỗi public method là một lời hứa; codebase lâu năm thường đau vì đã hứa quá nhiều lúc còn trẻ.