Learning interface in java

Posted by purencool on Thu, 27 Feb 2020 01:53:33 +0100

What is an interface

Interfaces are a set of specifications of behavior that appear to be a special abstract class from a grammatical perspective.
An interface is a standard that represents a capability and is often used to constrain the capabilities that its implementation class should have.

Definition of interface

Define interfaces using the interface keyword:
Syntax: interface interface name {}

interface MyInterface {
}

Features of methods and properties of interfaces

  1. Attributes in interfaces are public static constants, which are implicitly decorated with public static final s.
  2. Methods in interfaces prior to jdk1.8 were public abstract methods, implicitly decorated with public abstract
// Define interfaces
public interface Swimming {
	// Define Properties
	public static final int a = 20;
	
	// Even if omitted, public static final is implicitly used to decorate
	int b = 10;
	
	// Define abstract methods
	public abstract void youyong();
	
	// Implicitly decorate with public abstract
	void youyong2();
	
}
  1. Default and static methods can be defined in the interface after jdk1.8!
// Define interfaces
public interface Run {
	
	// Define abstract methods
	public abstract void work();
	
	// After jdk1.8, you can define static and default methods
	
	// Static method:
	public static void runAway() {
		System.out.println("I am a static method in an interface");
	}
	
	// Default method decorated with default
	default void escape() {
		System.out.println("I'm the default method in the interface");
	}
	
}

Differences between interfaces and abstract classes

abstract class Interface
Keyword abstract class interface
attribute No Requirements Open Static Constant
Method Abstract method, non-abstract method Abstract methods (default and static methods can be defined after jdk1.8)
Construction method Yes nothing
Inheritance mode single inheritance Allow multiple inheritance between interfaces

Multiple Inheritance Between Interfaces

Inherit other interfaces using the extends keyword; multiple interfaces are separated by a','sign

public interface Motion extends Run, Swimming {
	
	// Interfaces and interfaces allow multiple inheritance
	
	// You can see from the decompile tool that there is only one abstract method in this interface
	// But its implementation class must implement all the abstract methods it has with its parent interface!
	
	// In the interface, the default is public abstract void yundong();
	void yundong();
	
}

Implementation of interface

  1. Implement the interface using the implements keyword, with multiple interfaces separated by a','sign.
// Implement interfaces using the implements keyword, you can implement more
public class Student implements Run, Swimming{

	// All abstract methods in the interface must be implemented!
	@Override
	public void youyong() {
		System.out.println("Swimming with dog shaving");
	}

	@Override
	public void work() {
		System.out.println("Take a step that six parents don't recognize");
	}

}
  1. An implementation class becomes an abstract class if it does not implement all the abstract methods in the interface.
//Implement interfaces using the implements keyword, you can implement more
public abstract class Student implements Run, Swimming{

	// All abstract methods in the interface must be implemented!Otherwise, define itself as an abstract class!
	@Override
	public void youyong() {
		System.out.println("Swimming with dog shaving");
	}

//	@Override
//	public void work() {
//		System.out.println("Take a step that six parents don't recognize");
//	}

}

Interface Polymorphism

Declare an interface and instantiate its implementation class

public class DemoInterface {

	public static void main(String[] args) {
		
		// Interface polymorphism, declare interface types, instantiate implementation class objects
		Run student1 = new Student();
		
		// Default methods in interfaces use default-decorated methods
		student1.escape();
		
		// Abstract methods in interfaces
		student1.work();
		
		// Call static methods in interfaces
		Run.runAway();
		
		// Interface polymorphism, cannot call methods and properties unique to subclasses when polymorphic
		Swimming student2 = new Student();
		
		// Abstract methods in interfaces
		student2.youyong();
		
		// student2.work(); cannot call methods unique to subclasses
		
		// Instantiate Implementation Class Object
		Student student3 = new Student();
		
		// Method Call
		student3.escape();
		student3.work();
		student3.youyong();
		
	}
	
}

This is the summary of the initial learning about the interface.

Two original articles have been published. 1. Visits 29
Private letter follow

Topics: Attribute