Java learning notes Interface
I. Concepts and Definitions of interfaces
First, consider the definition of a simple interface:
public interface Output{ int MAX_LINE = 40; void out(); void getData(String msg);}
- Define the interface using the keyword interface
- The public parameter in front of the modifier interface can be omitted. If it is omitted, the default access control is used, that is, only the code in the same package structure can access this interface.
- The interface cannot have Constructor (different from the constructor in the class)
- All the Members in the interface, including constants and methods, are granted public access permissions. Therefore, when defining the members in the interface, you can omit the access control modifier.
- The attributes (member variables) in the interface can only be constants and all attributes are public, static, and final. Therefore, the following two statements in the interface are equivalent:
int MAX_LINE = 40;public static final int MAX_LINE = 40;
- The methods in the interface can only be abstract methods, that is, only the function type, name, and parameter list are declared, but specific implementations cannot be provided.
Ii. interface inheritance
Consider the following code:
1 interface A 2 { 3 int PROP_A = 5; 4 void testA(); 5 } 6 interface B 7 { 8 int PROP_B = 6; 9 void testB();10 }11 interface C extends A, B12 {13 int PROP_C = 7;14 void testC();15 }16 17 public class myJavaTest18 {19 public static void main(String[] args)20 {21 System.out.println(C.PROP_A);22 System.out.println(C.PROP_B);23 System.out.println(C.PROP_C);24 25 }26 }
Output:
5
6
7
- The inheritance of interfaces is different from that of classes. interfaces support multi-inheritance. Multiple parent interfaces are placed after the extends keyword. Multiple parent interfaces are separated by commas, as shown in the 11 rows above, sub-interface C inherits the parent interface A and B
- After the child interface inherits the class parent interface, it can obtain the access permission of their member constants. For example, row 21-23 can access the member constants PROP_A and PROP_ B of parent interface A and B through sub-interface C.
Iii. Implementation and definition of interfaces
Consider the following program:
1 interface Output 2 {3 int MAX_LINE = 4; 4 void out (); 5 void getData (String msg); 6} 7 8 interface Product 9 {10 int getProductTime (); 11} 12 13 // implement simple print job 14 public class Printer implements Output, Product15 {16 private String [] printData = new String [MAX_LINE]; 17 private int dataNum = 0; // record the number of tasks to be printed 18 19 public void out () 20 {21 while (dataNum> 0) 22 {23 System. out. println ("printer printing" + printData [0]); 24 25 // move the entire job queue to one place, and reduce the number of remaining jobs by 126 System. arraycopy (printData, 1, printData, 0, -- dataNum); 27} 28 29} 30 31 @ Override32 public int getProductTime () {33 // TODO Auto-generated method stub34 return 45; 35} 36 37 @ Override38 public void getData (String msg) 39 {40 if (dataNum> = MAX_LINE) 41 {42 System. out. println ("output queue full, failed to add"); 43} 44 else45 {46 // Add printed data to the queue, job count plus 147 printData [dataNum ++] = msg; 48} 49} 50 public static void main (String [] args) 51 {52 // create a Printer object, use 53 Output O = new Printer (); 54 O as Output. getData ("123"); 55 O. getData ("456"); 56 O. getData ("789"); 57 O. getData ("ABC"); 58 O. getData ("def"); 59 O. out (); 60 // create a Printer object and use 61 Product P = new Printer (); 62 System. out. println (P. getProductTime (); 63} 64}
Output:
Adding failed because the output queue is full
Printer print 123
Printer print 456
Printer print 789
Printer print ABC
45
- The interface cannot be used for instantiation, but the interface can declare variables of the reference type, such as 53 rows. The interface Output declares that the type of a referenced variable O is Output and 61 rows, the interface Product declares that the type of a reference variable P is Product.
- Classes are required for interface implementation. The common form is as follows:
[Modifier] class name extends parent class implements interface 1, interface 2 .. {class body part}
Implements is a keyword that indicates the implementation interface of this class. A class can implement multiple interfaces. Multiple Interfaces are separated by commas, as shown in the preceding program in line 14.
- A class can inherit a parent class and implement multiple interfaces at the same time. At this time, the implements part must be placed after the extends part.
- After a class declaration implements one or more interfaces, the class must fully implement all the abstract methods defined in these interfaces (abstract methods are those that are declared but not implemented ); otherwise, the class retains the abstract methods inherited from the parent interface, so the class must also be defined as an abstract class.
The above program implements the Output interface and the Product interface using the Printer class. Therefore, the Printer object can be assigned to both the Output variable (53 rows) and the Product variable (61 rows ), it seems that the Printer class is both a subclass of the Output class and a subclass of the Output class. This is the legendary multi-inheritance of Java simulation.
JAVA Study Notes
I have read this book
You just need to watch it.
It was written by Lin Xinliang.
Well written
I suggest you keep this book
It will be better to combine the Basic java data.
Is JAVA learning notes suitable for basic learning?
I have seen this book in the bookstore and library, and I feel that I am not suitable for using this book. Of course this is not absolute. Maybe you can. I am a beginner in JAVA and use this book as a very basic and absolutely zero basis. Almost reached the hand level. Furthermore, JAVA Object-Oriented Programming is also very good. This book provides a comprehensive explanation of JAVA language points.