Sunday, October 17, 2010

Java Vs Objective C: From Interface to Protocol

In my previous blog entry, I compared Java method invocation and Objective C messaging. Today I am writing about Objective C protocols.

Objective C offers something called protocol which is equivalent to Java Interface. An interface is a contract which you can implement in a class so that object of that class conforms to this contract.

A Java Interface is defined using interface keyword. For example in AWT library of JAVA a MouseListener is an interface which declares method for mouse handling, something like this:

package java.awt.event;
public interface MouseListener extends EventListener {

    /**
     * Invoked when the mouse button has been clicked (pressed
     * and released) on a component.
     */
    public void mouseClicked(MouseEvent e);

    /**
     * Invoked when a mouse button has been pressed on a component.
     */
    public void mousePressed(MouseEvent e);

    /**
     * Invoked when a mouse button has been released on a component.
     */
    public void mouseReleased(MouseEvent e);

    /**
     * Invoked when the mouse enters a component.
     */
    public void mouseEntered(MouseEvent e);

    /**
     * Invoked when the mouse exits a component.
     */
    public void mouseExited(MouseEvent e);
}


This interface you can implement in a class and then implement the methods, something like this:


class myMouseHandler implements MouseListener {
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}

  //other methods are omitted

}



Objective C protocols:

Similarly in Objective C, protocol declares a set of methods which one can implement in a class to conform to the protocol contract. For example Objective C (and iOS) has UIApplicationDelegate protocol which specifies set of methods for handling application lifecycle and these can be implemented by an iOS application so that it can receive these method calls and handle them appropriately. Some methods of this protocol are:


@protocol UIApplicationDelegate<NSObject>

@optional

- (void)applicationDidFinishLaunching:(UIApplication *)application;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);

- (void)applicationDidBecomeActive:(UIApplication *)application;
- (void)applicationWillResignActive:(UIApplication *)application;
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;  // no equiv. notification. return NO if the application can't open for some reason

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;      // try to clean up as much memory as possible. next step is to terminate app
- (void)applicationWillTerminate:(UIApplication *)application;




To implement a protocol in a class you specify protocol as part of your class declaration, for example in the header file (.h) you declare that you are implementing UIApplicationDelegate:


@interface SampleAppDelegate : NSObject <UIApplicationDelegate> {
}



And then in class implementation (.m file) you can implement the method of UIApplicationDelegate, something like this:



@implementation SampleAppDelegate

#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
//implement it here
}



For more details on protocol, see this.



Promote your blog

No comments:

Post a Comment