Interfaces are implemented using protocols in Objective-C. Here’s an example demonstrating the concept:
In this example, we define a Geometry protocol (which is similar to an interface) with area and perim methods.
We then implement this protocol on Rect and Circle classes. Each class provides its own implementation of the area and perim methods.
The measure function takes any object that conforms to the Geometry protocol and calls its area and perim methods.
In the main function, we create instances of Rect and Circle and pass them to the measure function.
To run this program, save it as Interfaces.m and compile it with:
The output will be similar to:
This example demonstrates how Objective-C uses protocols to achieve interface-like behavior, allowing for polymorphism and abstraction in object-oriented design.