Thursday, June 21, 2012

interface keyword in Java and ObjectiveC

There is an interface keyword in both ObjectiveC and Java (in Objective-C it is @interface). Are they used for the same purposes ?

Answer: No.

We will see this in detail.

interface keyword in Objective-C


In C, a function (that is accessed by a different file) is usually written in two files. In .c and .h files. We write the function prototype in .h file and the function body in the .c file. 
The .c files, of course with the help of .h files, is compiled to form the libraries (.a, .o, .so etc. ) and executable where as the .h files are used by the code that uses these libraries. 
The .h files thus act as a document for anybody who is using this library. By checking the .h file one can understand what APIs this library offers. The .h files also help the compiler to understand the symbols (function name, variable names etc ) of the external libraries that you are using in the code. 

Objective-C also adopts the same philosophy. 

A class in Objective-C is , usually, is a combination of a .m and a .h file. 

Here, the .m file is used to write the function body in a class, where as the .h file is used to write the function prototype (or interface), variable declaration, mention class inheritance etc.  

In .h file the function declaration are mentioned between the keywords @interface and @end and in .m file the function bodies are mentioned between the keywords @implementation and @end

Example


.h file

//SampleClass inherits from NSObject
@interface SampleClass : NSObject

// Variables are declered between these curly braces. 
{
    int value1;
}

// Setter for value1
-(void)setValue1:(int)v;


//Getter for value1. Note that there is no 'get' word in the getter. By convention, in 
// Objective-C the getter function name is same as the variable name. 
-(int)value1;

@end

.m file

#import "SampleClass.h"

//Implemetation of the prototype defined in SampleClass.h
@implementation SampleClass


//Implementation of setter
-(void)setValue1:(int)v{
    value1 = v;
}


//Implementation of getter
-(int)value1{
    return value1;
}

@end


interface keyword in Java


interface in Java is used to enable dynamic binding and thereby polymorphism. interface is just an agreement that defines some function prototype. A class using this interface can take any object who is adhering to this agreement. Any class can adhere to the agreement by just implementing the function prototype of the interface.

Example

Interface (Agreement)
// This interface is a agreement. Any class can adhere to this agreement by 
//implementing this interface. 

public interface SampleInterface{
// You can have more function in this interface. 
    void printValue();

}


Implementation (A Class adhering to the above agreement)
public class SampleImplementationOne implements SampleInterface{
    
    void printValue(){
        System.out.println("This is SampleImplementationOne");
    }
}


User - This class uses the interface SampleInterface. Note:- Nowhere in the class there will be mention of any of the implementation of this interface (such as  SampleImplementationOne). There is only reference of SampleInterface. The function printLogValue can take any class that implements the interface SampleInterface.
public class UserClass{

    void printLogValue(SampleInterface sample){
        sample.printValue();
    }
}
If we pass SampleImplementationOne to the function printLogValue, it will print "This is SampleImplemetationOne".
public static void main(String[] args){

    UserClass uc = new UserClass();
    uc.printLogValue(new SampleImplementationOne());
    return;

}
The class UserClass decides which implementation of SampleInterface is passed at the run time. This is a powerful feature and you can decide which implementation class to use at the run time.

Consider the following case.

Suppose you are implementing a network communication module. It helps if you define an interface (NetworkAdapter) that will declare prototypes for send and receive functions. This way you can choose your implementation of NetworkAdapter for Bluetooth, Wifi or any other interface at runtime.

The interface also help in development. Suppose the code of both Bluetooth or WiFi is not ready when you start development of your communication module. You can write your own DummyAdapter (implementing NetworkAdapter) for testing. When the Bluetooth or WiFi is ready, you can change the DummyAdapter to the corresponding implementation.

Dynamic binding in Objective-C

Now you see that the Java make use of the powerful dynamic binding with the use of interface keyword. How does Objective-C make use of dynamic binding? With the use of @protocol keyword. The detail of which is out of the scope of this article. See here for more details on protocol.


No comments:

Post a Comment