. 1 Package Interface; 2 . 3 / * . 4 how to define an interface: . 5 public interface Interface Name { 6 // interface content 7 } 8 * / . 9 10 / * 11 using the interface step: 12 1 interface can not be used directly, there must be a "implementation class" to "realize" the interface 13 format: 14 public class implements the interface implementation class name {name 15 // .... 16 } 17 class that implements the interface must overwrite 2 (implement) the interface All the abstract methods, 18 to achieve: to remove the abstract keyword, plus the method body braces. 19 3. Create an object implementation class, use. 20 21 Note: 22 If the class does not implement all of the abstract methods interface overwritten, then the implementation class and that it must be abstract class. 23 is * / 24 public class Demointerstate { 25 public static void main (String [] args) { 26 is // Error writing can not directly use the new interface object 27 28 // create objects using implementation class 29 MyinterfaceAbstrctImpl impl = new MyinterfaceAbstrctImpl (); 30 impl.methodAbs (); 31 is } 32 }
. 1 Package Interface; 2 . 3 public class MyinterfaceAbstrctImpl the implements MyinterfaceAbstract { . 4 @Override . 5 public void methodAbs () { . 6 System.out.println ( "This is the first method!" ); 7 } 8 }
. 1 Package Interface; 2 / * . 3 * in any version of Java, the interface can define the abstract methods. 4 * format: . 5 * public abstract method return value name (parameter list); 6 * Note: 7 * abstract method 1. Among the interfaces, the modifier must be fixed two keywords: public abstract . 8 * 2. both keywords modifiers, may optionally be omitted (not recommended) . 9 * / 10 public interface MyinterfaceAbstract { . 11 // this is an abstract method 12 is public abstract void methodAbs (); 13 is }