4

Is it possible to have two independent class communicate by implementing an interface, and if so, how?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Med
  • 73
  • 1
  • 1
  • 4
  • `interface I1 { void foo(); } interface I2 { void bar(I1 i1); }`? – Luiggi Mendoza May 08 '13 at 14:23
  • Your question isn't very clear. Maybe you could tell us the problem you are trying to solve? – Kevin Brydon May 08 '13 at 14:24
  • Yes, it's possible. Both classes implement the interface. The interface specifies methods that facilitate communication. – Gilbert Le Blanc May 08 '13 at 14:31
  • What are they trying to communicate? – Richard Tingle May 08 '13 at 14:32
  • i want to pass a string from one class to another class using java interface , and the classes are independentes – Med May 08 '13 at 14:33
  • Why must an interface be used, is the two classes an example and in reality its many classes – Richard Tingle May 08 '13 at 14:33
  • @Richard Tingle : nop in reality i have juste two classes that resides in two differents paquages – Med May 08 '13 at 14:36
  • Ok, but are they treated the same as each other, or does A want to talk to B and B talk to A (but A talking to A and B talking to B would be different or not occur). If its just A-->B and B-->A i'm not sure interfaces are really nessissary – Richard Tingle May 08 '13 at 14:45
  • It's kinda A-> B and B -> A , i think that pipes and sockets would do this job , but the two business process represented by the two classes must be independent , – Med May 08 '13 at 14:49

1 Answers1

18

I think that using interfaces is a little extreme here but i'm assuming this is a simplification of a larger problem

public interface SomeInterface {

    public void giveString(String string);

    public String getString();

}


public class Class1 implements SomeInterface{

    String string;

    public Class1(String string) {
        this.string = string;
    }

    //some code

    @Override
    public void giveString(String string) {
        //do whatever with the string
        this.string=string;

    }

    @Override
    public String getString() {
        return string;
    }



}


public class Class2 implements SomeInterface{

    String string;

    public Class2(String string) {
        this.string = string;
    }

    //some code

    @Override
    public void giveString(String string) {
        //do whatever with the string
        this.string=string;

    }

    @Override
    public String getString() {
        return string;
    }

}


public class Test{

    public static void main(String args[]){
        //All of this code is inside a for loop
        Class1 cl1=new Class1("TestString1");
        Class2 cl2=new Class2("TestString2");

        //note we can just communicate now, no interface
        cl1.giveString(cl2.string);


        //but we can also communicate using the interface
        giveStringViaInterface(cl2,cl1);
    }

    //any class that extended SomeInterface could use this method
    public static void giveStringViaInterface(SomeInterface from, SomeInterface to){
        to.giveString(from.getString());
    }



}
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
  • I think that this example would be very helpfull , Thanks a lot Richad Tingle for your responses , – Med May 09 '13 at 10:29