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

Sunday, October 3, 2010

Java Vs Objective C: Object Method Invocation to Object Messaging

I recently started looking into iphone and ios app development. The primary language for ios based applications is Objective C, which is an object oriented C like language. Coming from Java background there are certain aspects and concepts in the Objective C language, which are different for a Java programmer.

So here I am starting a series of blog posts, to give an overview of Objective C language and contrast it with Java programming language.

In this blog, I want to compare Java object method invocation to Objective C object messaging. They are pretty much doing the same thing.

In Java you define a class and methods which can be invoked on the object of this class. For example let say you have a class called Window and it has a method called getBounds which returns Rectangle bounds of the Window. It can looks something like this:
public class Window {
private Rectangle myBounds;

Rectangle getBounds() {
return this.myBounds;
}

}

And this is how you invoke a method on an object:
Window window = new Window();
Rectangle rect = window.getBounds();

In Objective C you will define a class by first defining an interface in the header file. Here in Window interface we have defined a method called bounds:
@interface Window : NSObject {
CGRect *rect;
}
- (CGRect*) bounds;

and then in class file you define the implementation:
@implementation Window
- (CGRect*) bounds {
return rect;    
}

@end

Objective C messaging:
In objective C to call a method of an object you do it by messaging an object.A message is the method signature, along with the parameter information the method needs.Messages are enclosed by brackets ([ and ]). Inside the brackets, the object receiving the message is on the left side and the message (along with any parameters required by the message) is on the right.

For example to invoke bounds method on an instance of Window object you use messaging like shown below:
//create an instance of Window by messaging alloc on Window class. alloc is like static
//method on Window class
Window *window = [Window alloc];
//now call bounds on window
CGRect *rect = [window bounds];


This shows how messaging works in Objective C. In future blogs we will look at other features of Objective C language.

Promote your blog