Interface
1. interface modifier
Reference data types: class, array, interface.
Interface means an interface.
2. Interface Overview
Interface is another reference data type besides class and array, which will eventually be compiled into. Class file. Interface is not a class.
3. Interface features
- The definition class uses the keyword class, and the definition interface uses the keyword interface
- The properties in the interface are public static constants
- The methods in the interface are all abstract methods
- Abstract methods in interfaces can be modified without using the abstract modifier
- The attributes in the interface are decorated with public static final by default
For example:
//This is an interface public interface Action{ int number = 1; //This is a static constant //public static final int number; void start(); //This is an abstract method //public abstract void start(); }
4. Differences between interfaces and classes
(1) The interior of the class encapsulates member variables, construction methods and member methods, while the interior of the interface mainly encapsulates methods (abstractions) and static constants.
(2) Class uses the keyword class and interface uses the keyword interface
5. New features of JDK8
(1) Allows you to write static and default methods in an interface
(2) Allows private methods to be written in an interface
For example: interface with static method and default method (JDK8)
public interface Action{ //This is the default method public default void run1(){ //Execute statement } //This is a static method public static void run2(){ //Execute statement } //This is a private method private void run(){ //Execute statement } }
6. Interface implementation
5.1 general
The relationship between classes is inheritance, and the relationship between classes and interfaces is implementation. A class can implement one or more interfaces. Class to implement the interface, you must override the abstract methods in the interface.
5.2 implements keyword
The keyword of class and class inheritance relationship is extends, and the implementation relationship between class and interface is to use the implements keyword.
5.3 realization
1. Implement an interface
For example:
public interface Action{ void run(); void sayHello(); } //To define a class implementation interface, you must override the methods in the interface. If you don't want to override, define this class as an abstract class abstract class Student implements Action{ }
2. Implement multiple interfaces
//Action interface public interface Action{ void run(); void sayHello(); }
//Mark interface public interface Mark{ void star(); }
Implement the two interfaces, separated by commas, and implement all abstract methods in the interface
public class Student implements Action,Mark{ @Override public void star() { // TODO Auto-generated method stub } @Override public void run() { // TODO Auto-generated method stub } @Override public void sayHello() { // TODO Auto-generated method stub } }
7. Interface inheritance
7.1 relationship
There is multiple inheritance between interfaces
For example:
public interface Runable { void run(); }
public interface Flyable { void fly(); }
//The interface Action inherits the Runable interface and the Flyable interface public interface Action extends Runable,Flyable{ void doSomething(); //Methods in interface Action }
//Because the Action interface inherits the other two interfaces, there are also methods in the other two interfaces in the Action interface public class Demo implements Action{ @Override public void run() { // TODO Auto-generated method stub System.out.println("Run fast..."); } @Override public void fly() { // TODO Auto-generated method stub System.out.println("Flying thief, come on..."); } @Override public void doSomething() { // TODO Auto-generated method stub System.out.println("Lala Lala..."); } }
8. Interface polymorphism
premise
The premise of polymorphism is inheritance. There must be a child parent relationship first. In fact, the implementation relationship between classes and interfaces is also a form of inheritance. Therefore, polymorphism can also be used in the implementation relationship between classes and interfaces
For example:
public interface Action { void run(); } class Student implements Action{ @Override public void run() { // TODO Auto-generated method stub System.out.println("The students run fast.."); } } class Teacher implements Action{ @Override public void run() { // TODO Auto-generated method stub System.out.println("The teacher runs fast.."); } } class Test{ public static void main(String[] args) { Action a; //Declare a reference to the interface //The interface reference points to any implementation class object. Here, it points to the Student class a = new Student(); //The interface reference calls the rewritten method. Note: what is called here is the method rewritten in the Student class a.run(); //The interface reference points to any implementation class object. Here, it points to the Teacher class a = new Teacher(); //The interface reference calls the rewritten method. Note: what is called here is the method rewritten in the Teacher class a.run(); } }
Keep it for review...