I have just initially learned the interface of java, and I will write down my gains here:
package com.funyoo.about_interface; public interface MyFirstInterface { public static final int red = 1; int blue = 2; public abstract void func(); int fun(); }
Define the first interface: MyFirstInterface
Note: The interface has the following "iron law": the interface will be jointly decorated with public static final to its members;
Its methods will be decorated with public abstract union
Therefore, the above member blue is also modified by public, static, final, and the method fun() is also modified by public, abstract.
package com.funyoo.about_interface; public interface MySecondInterface extends MyFirstInterface { public static final int red = 2; public int fun2(); }
This is the second interface defined, MySecondInterface, which inherits the first interface MyfirstInterface;
package com.funyoo.about_interface; public interface MyThirdInterface extends MyFirstInterface { int red = 3; }
Define the third interface, MyThirdInterface, which also inherits the first interface MyfirstInterface;
Now we create a new class to implement the first interface: ImplementensMyfirstInterface
package com.funyoo.about_interface; public class ImplemensMyfirstInterface implements MyFirstInterface { @Override public void func() { } @Override public int fun() { return 0; } }
Because the methods created by the interface are abstract methods, when they are implemented, the abstract methods should also be implemented.
Next we create a new class to implement all the interfaces: ImplementensMyInterface
package com.funyoo.about_interface; public class ImplemensMyInterface implements MyFirstInterface, MySecondInterface, MyThirdInterface { @Override public int fun2() { return MyFirstInterface.red; } @Override public void func() { // TODO auto-generated method stub } @Override public int fun() { return MyFirstInterface.red; } }
Let's create a main class to conduct an experiment:
package com.funyoo.about_interface.test; import com.funyoo.about_interface.ImplemensMyInterface; import com.funyoo.about_interface.ImplemensMyfirstInterface; import com.funyoo.about_interface.MyFirstInterface; import com.funyoo.about_interface.MySecondInterface; import com.funyoo.about_interface.MyThirdInterface; public class Test { public static void main(String[] args) { ImplemensMyInterface imi = new ImplemensMyInterface(); ImplemensMyfirstInterface imf = new ImplemensMyfirstInterface(); // Equivalent to // MyFirstInterface imi = new ImplemensMyInterface(); // MyFirstInterface imf = new ImplemensMyfirstInterface(); System.out.println(imi instanceof MyFirstInterface); //true //*** System.out.println(imi instanceof MySecondInterface); //true System.out.println(imi instanceof MyThirdInterface); //true System.out.println(imi instanceof ImplemensMyInterface); //true System.out.println("--------------------------------"); System.out.println(imf instanceof MyFirstInterface); //true //*** System.out.println(imf instanceof MySecondInterface); //false System.out.println(imf instanceof MyThirdInterface); //false System.out.println("---------------------------------"); System.out.println(imi.getClass()); //class com.funyoo.about_interface.ImplemensMyInterface System.out.println(imf.getClass()); //class com.funyoo.about_interface.ImplemensMyfirstInterface } }
Judging from the above program and running results, imi and imf belong to two different classes, but are linked because of an interface;
Look at the running results of lines 19 and 26
We can boldly change both ImplemensMyInterface and ImplemensMyfirstInterface on lines 12 and 13 to:
MyFirstInterface
The result of running the program remains unchanged!
So in the future programming, we can "link" two completely different classes with one interface!