title: Simulate interface in java with objective-c
body: <p>I came from a java background, and I was trying to use protocol like a java interface.</p>
  
  <p>In java you can have an object implement an interface and pass it to a method like this:</p>
  
  <pre><code>public interface MyInterface {
   void myMethod();
  }
  
  public class MyObject implements MyInterface {
   void myMethod() {// operations}
  }
  
  
  
  public class MyFactory {
    static void doSomething(MyInterface obj) {
       obj.myMethod();
    }
  }
  
  public void main(String[] args) {
   // get instance of MyInterface from a given factory
   MyInterface obj = new MyObject();
   // call method
   MyFactory.doSomething(obj);
  }
  </code></pre>
  
  <p>I was wondering if it's possible to do the same with objective-c, maybe with other syntax.</p>
  
  <p>The way I found is to declare a protocol</p>
  
  <pre><code>@protocol MyProtocol
  -(NSUInteger)someMethod;
  @end
  </code></pre>
  
  <p>then my object would "adopt" that protocol and in a specific method I could write:</p>
  
  <pre><code>-(int) add:(NSObject*)object {
   if ([object conformsToProtocol:@protocol(MyProtocol)]) {
     // I get a warning
     [object someMethod];
   } else {
     // other staff
   }
  }
  </code></pre>
  
  <p>The first question would be how to remove the warning, but then in any case other caller could still pass wrong object to the class, since the check is done inside method.
  Can you point out some other possible way for this ?</p>
  
  <p>thanks
  Leonardo</p>