1 Package Com. Action; 2 3 Public Interface Interfacetest { 4 5 Public Abstract Void Fun1 (); 6 7 Void Fun2 (); // The default values are public and abstract. 8 9 Void Fun3 (); 10 11 Int I = 10; // All are public static final by default. 12 }
1 Package Com. Action; 2 3 Public Class Test1 Implements Interfacetest { 4 5 /** 6 * @ Param ARGs 7 */ 8 Public Static Void Main (string [] ARGs ){ 9 // Todo auto-generated method stub 10 Test1 test1 = New Test1 (); 11 Test1.fun1 (); 12 System. Out. println (test1. I ); 13 14 } 15 16 @ Override 17 Public Void Fun1 (){ 18 // Todo auto-generated method stub 19 System. Out. println ("fun1" ); 20 } 21 22 @ Override 23 Public Void Fun2 (){ 24 // Todo auto-generated method stub 25 System. Out. println ("fun2" ); 26 } 27 28 @ Override 29 Public Void Fun3 (){ 30 // Todo auto-generated method stub 31 System. Out. println ("fun3" ); 32 } 33 34 }
1 Package Com. Action; 2 3 Public Abstract Class Abstractclass { 4 5 Void Fun1 (){ 6 System. Out. println ("fun1" ); 7 } 8 Public Abstract Void Fun2 (); 9 10 Public Int I = 01 ; 11 }
1 Package Com. Action; 2 3 Public Class Test2Extends Abstractclass { 4 5 /** 6 * @ Param ARGs 7 */ 8 Public Static Void Main (string [] ARGs ){ 9 // Todo auto-generated method stub 10 Test2 Test2 = New Test2 (); 11 Test2.fun1 (); 12 Test2.fun2 (); 13 Test2. I = 2 ; 14 System. Out. println (test2. I ); 15 } 16 17 @ Override 18 Public Void Fun2 (){ 19 // Todo auto-generated method stub 20 System. Out. println ("fun2" ); 21 } 22 23 }
1. abstract class represents an inheritance relationship in Java. A class can only use an inheritance relationship once. However, a class can implement multiple interfaces.
2. In abstract class
You can have your own data members or non-Abstarct member methods. In the interface, you can only have static data members that cannot be modified (that is, they must be
Static final, but data members are generally not defined in the interface). All member methods are abstract.
3. abstract class and interface have different design concepts. Abstract class represents the "is-a" relation, and interface represents the "like-a" relation.
4. The class implementing the interface must implement all the methods in it, and the class implementing the abstract class must implement the abstract methods in it, rather than the abstract methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface.
5. the variables defined in the interface are of the public static final type by default and must be given the initial values. Therefore, the Implementation class cannot be redefined or changed.
6. the variables in the abstract class are friendly by default. Their values can be redefined in the subclass or assigned again.
7. The methods in the interface are public and abstract by default.