JAVA faces objects (5) -- interfaces and java faces object interfaces
An interface consists of global constants and common abstract methods. The interface definition format is as follows:
1 interface name {2 Global constant; 3 abstract method; 4}
Abstract methods in the interface must be defined as public access permissions. If this parameter is not specified in the interface, the default value is public access.
Interface Definition:
1 interface A {2 public static final String AUTHOR = "Zhang Yu"; // defines the global constant 3 public abstract void print (); // define the abstract Method 4 public abstract String getInfo (); // define the abstract Method 5}
However, the interface definition has declared that the interface is composed of global constants and abstract methods, so it can be simplified to the following format:
1 interface A {2 String AUTHOR = "Zhang Yu"; // equivalent to: public static final String AUTHOR = "Zhang Yu"; 3 void print (); // equivalent: public abstract void print (); 4 String getInfo (); // equivalent to: public abstract String getInfo (); 5}
Like an abstract class, to use an interface, you must also use a subclass. The subclass uses the implement keyword to implement the interface:
1 class subclass implement interface A, interface B,... {2}
Implementation interface:
1 interface A {2 public String AUTHOR = "Zhang Yu"; 3 public void print (); 4 public String getInfo (); 5} 6 interface B {7 public void say (); 8} 9 class X implements A, B {// sub-class simultaneously implements two interfaces 10 @ Override11 public void say () {// override the abstract Method 12 System in interface B. out. println ("Hello World"); 13} 14 @ Override15 public void print () {// override the abstract Method 16 System in interface. out. println ("name:" + AUTHOR); 17} 18 @ Override19 public String getInfo () {// override the abstract Method 20 return "Hello" in interface "; 21} 22} 23 public class Demo {24 public static void main (String [] args) {25 X x = new X (); 26 x. say (); // call the method that has been overwritten 27 x. print (); 28} 29}
In addition, interfaces can be inherited and inherited.
1 interface sub-interface extends parent interface A, parent interface B,... {2}
If a subclass inherits the abstract class and the interface, the following format is available:
1 class subclass extends abstract class implement interface A, interface B,... {2}