Thursday, 13 March 2008

Java Interface concept example

========= Example 1 =================
**** GuguInter.java ***
interface GuguInter {
public static final int input1 = 2;
public static final int input2 = 3;
public void printGugu2();
pulbic void printGugu3();
}

***** PrintGuguClass.java ******

public class PrintGuguClass implements GuguInter {
PrintGuguClass c = new PrintGuguClass();
c.printGugu2();
c.printGugu3();
}

public void printGugu2() {
System.out.println(GuguInter.input1+ " printed");
}

public void printGugu3() {
System.out.println(GuguInter.input2+ " printed");
}

}

============ Example 2 ====================
****** myInterface.java **********
interface myInterface {
public abstract void method();
}

**** CB.java ***********
class CB implements myInterface {
public void method() {
System.out.println("method in CB");
}
}

***** CC.java **********
class CC implements myInterface {
public void method() {
System.out.println("method in CC");
}
}

***** CA.java ********
class CA {
public void method(myInterface mi) {
my.method()
}
}

***** Cmain.java *****
class Cmain {
public static void main(String[] args) {
CA ca = new CA();
ca.method(new CB());
ca.method(new CC());
}
}