What is the equivalent to protected methods in Objective-C? I want to define methods which only the derived classes may call/implement.
There are two protected methods in Object : clone() and finalize() . finalize() is not intended to be called by client code, but may be overridden by subclasses - thus, it is protected.
A protected method is like a private method in that it can only be invoked from within the implementation of a class or its subclasses. It differs from a private method in that it may be explicitly invoked on any instance of the class, and it is not restricted to implicit invocation on self .
The method displayed in class A is protected and class B is inherited from class A and this protected method is then accessed by creating an object of class B.
Notes: You cannot declare a class as protected . Only the methods or fields within a class can have this access modifier.
You can simulate protected and private access to methods by doing the following:
These protections are not, as Sachin noted, enforced at runtime (as they are in Java, for example).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With