Java Basics: Interfaces
introduce
Interfaces in Java are defined as abstract types used to specify the behavior of classes that are implemented. Java interfaces include static constants and abstract methods, as well as default and static methods after 1.8. A class can implement multiple interfaces. In Java, interfaces are declared using the interface keyword. Before JDK8, all methods in the interface were implicitly exposed and abstract. After 1.8, static methods and methods with a method body declared with default can be defined in the interface.
graph LR; Interface - static constant interface - Abstract method interface - default method interface - static method
use
Extract the common features of a series of objects and declare methods for unified implementation. For example, both cats and mice are animals and can crawl or bark. At this time, we define an animal interface, declare public abstract methods of crawling and calling, and create cat and mouse classes to implement the animal interface
Advantages: When adding a common abstract method, such as a fur color method to the animal interface, we don’t have to use our brains to remember how many animals implemented it, because compile-time errors will be reported, and we can clearly add methods to the animal implementation class without worrying about missing an animal without a fur color
Code implementation:
interface Animal {
public void move(a);
}
class Cat implements Animal {
public void move(a){
System.out.println("The way cats walk..."); }}class Dog implements Animal {
public void move(a){
System.out.println("The way a dog walks..."); }}Copy the code
Add a coat colour to the animal
interface Animal {
/ /... Other methods
public void color(a);
}
Copy the code
The Cat and Dog classes will compile an error if they do not override the color method
New feature :: Default method
The previous interface backward compatibility is not good, for example, I want to add a how to eat the animal in the animal class, so it cannot be implemented in every animal, so it needs this method can not implement this method
interface Animal {
default void howToEat(a){
System.out.println("Animal: A class that implements this interface may not implement this method."); }}Copy the code
Neil: So, if you have a rabbit class that implements Animal and implements Mammal, and both interfaces contain the howToEat method
interface Mammal {
default void howToEat(a){
// Classes that implement this interface may not implement this method
System.out.println("Keepers: Eating methods of mammals..."); }}public class Rabbit implements Animal.Mammal{}Copy the code
The above code failed to compile as follows:
At this point we need to rewrite the howToEat method
public class Rabbit implements Animal.Mammal {
@Override
public void howToEat(a) {
System.out.println("Spicy rabbit head"); }}Copy the code
New features: static methods
Suppose I want to get the number of legs of a mammal, and I don’t want to get its implementation class, or how many legs or claws each implementation class has
public interface Mammal {
static int getLegByName(String name) {
switch (name) {
case "Rabbit":
return 4;
case "Duck":
return 2;
default:
return 0; }}}Copy the code
conclusion
Conclusion:
- Interfaces anchor the implementation characteristics of an implementation class for a specified method
- New in JDK8, you can define static and default methods in an interface