Saturday, September 13, 2008

WTF is an Interface?

A lot of people are confused on WTF an interface or what is its usability.

An interface is basically a reference type that has abstract (or only has names but no implementation) members that must be implemented by the inheriting class. In simpler words you just make and interface and all its members (methods) must have names, accessor, return/parameters but you don’t write any code for it AND the first concrete class that implements it must write the method’s content for it if it is not implemented already.

WTF is it good for? Ok imagine you and a team is working on a very large project so each member makes his/her own component. Even if they are able to implement and follow a naming convention chances are there will still be some problems in creating a generic ode that can handle or the coding. Here is an example

public class noInterface {

/* rest of code omitted for clarity */

public printThis(Object toBePrinted) {

toBePrinted.Print();

}

}

Problem solved? Not really since you cannot call the Print method from an object. You can go about this another way such overload the printThis method but if you have to create all the methods for each type you pass this beats the reuse concept of OOP. Another possibility is to use reflection on the object but this is not the way since this is against the KISS approach. A smart ass answer would be “why not find a language that allows reflection etc etc etc” well I must admit there are languages out there that complies with this but still there is no way of knowing whether the object passed has the print method without checking for it explicitly

WTF are we supposed to do? use an interface. Here is what I mean

create an interface e.g

public interface printInterface {

void print();

}

create classes that implement that interface e.g.

public class Test1 implements printInterface{

public void print() {

System.out.println(”test1″);

}

}

//

public class Test2 implements printInterface {

public void print() {

System.out.println(”test2″);

}

}

and an example run is

public class Main {

public static void main(String[] args) {

Test1 xxx = new Test1();

Test2 xxxx = new Test2();

/* an alternative will be

printInterface xxx = new Test1() ; // this is allowed since Test1 implemented printInterface

printInterface xxxx = new Test2() ; // this is allowed since Test2 implemented printInterface

end of alternative

*/

Main yyy = new Main();

yyy.doPrintInterface(xxx);

yyy.doPrintInterface(xxxx);

}

public void doPrintInterface(printInterface z){

z.print();

}

}

Now if the object passed to the doPrinterInterface method did not implement the printinterface interface then a compile time error will be detected, you can pass any object to the method and be assured that it will have the print method.

Why not use an abstract call instead? There are many reasons but one of them is that several languages don’t allow multiple inheritance from classes but allows it from interfaces. Yes you can implement multiple interfaces in Java and C#.

Note: I used java

for a C# example kindly see http://www.codeguru.com/csharp/csharp/cs_syntax/interfaces/article.php/c7563

No comments: