java - Two independent classes that communicate using an Interface -
is possible have 2 independent class communicate implementing interface, , if so, how?
i think using interfaces little extreme here i'm assuming simplification of 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 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 string this.string=string; } @override public string getstring() { return string; } }
public class test{ public static void main(string args[]){ //all of code inside loop class1 cl1=new class1("teststring1"); class2 cl2=new class2("teststring2"); //note can communicate now, no interface cl1.givestring(cl2.string); //but can communicate using interface givestringviainterface(cl2,cl1); } //any class extended someinterface use method public static void givestringviainterface(someinterface from, someinterface to){ to.givestring(from.getstring()); } }
Comments
Post a Comment