Author Resume: zoro-1, currently a freshman, learning Java, data structures, etc.
Author Home Page:zoro-1’s homepage
You are welcome to like Favorite Add attention Oh!
Java interfaces
The concept of interfaces
In real life, examples of interfaces abound, e.g., USB ports on laptops, power outlets, etc.
On the USB port of the computer, you can plug in: USB flash drive, mouse, keyboard… all devices that comply with the USB protocol On the power outlet jack, you can plug in: computer, TV, rice cooker… all devices that comply with the specifications can be seen by the above example:An interface is a public specification of behavioral standards, which can be commonly used by everyone in the implementation, as long as it meets the specification standards. In Java, an interface can be seen as: a public specification for multiple classes, a reference data type.
grammatical rule
The interface syntax rules include the following:
-
The name of the interface must conform to the naming rules for identifiers, i.e., it can only consist of letters, numbers, and underscores, and cannot begin with a number.
-
Interface definitions use the keywordinterface。
-
Method declarations in interfaces do not require specific implementations, only method names, parameter lists, and return value types need to be defined.
-
Method names follow the naming rules for identifiers and must begin with a capital letter.
-
Methods in interfaces are not allowed to contain code blocks, and constructor methods.
-
Methods in interfaces can have parameter lists of basic data types, class types, interface types, and array types.
-
Interfaces can inherit from multiple interfaces, using the extends keyword.
-
A class can implement multiple interfaces, using the implements keyword.
-
A class that implements an interface must implement all the methods in the interface.
-
Methods in interfaces are public by default, but you cannot use access modifiers to modify their access.
Interface Characteristics
Interface Use Cases
Here’s a simple example of a Java interface that defines an Animal interface with two methods, eat() and sleep():
public interface Animal {
void eat();
void sleep();
}
We can then create a Dog class to implement the Animal interface:
public class Dog implements Animal {
public void eat() {
System.out.println("Dog is eating");
}
public void sleep() {
System.out.println("Dog is sleeping");
}
}
Note that the Dog class must implement all the methods defined in the Animal interface. Next, we can create a Main class to test the Dog class:
public class Main {
public static void main(String[] args) {
Animal animal = new Dog();
animal.eat();
animal.sleep();
}
}
In this test class, we first create a variable of type Animal, animal, and instantiate it as a Dog object. Then we call animal’s eat() and sleep() methods, which automatically call the methods implemented in the Dog class.
Through this simple example, we can see how interfaces are used and how they are implemented, which helps us to better understand the concepts and roles of interfaces.
Multiple Inheritance of Interfaces
In some programming languages, an interface can inherit from more than one interface, also known as multiple inheritance of interfaces. This allows you to implement a new interface by inheriting from multiple interfaces, thus giving it the characteristics and behavior of multiple parent interfaces.
interface Interface1 {
void method1();
}
interface Interface2 {
void method2();
}
interface Interface3 extends Interface1, Interface2 {
void method3();
}
In this example, the interfaceInterface3
inheritedInterface1
andInterface2
method, and added a method of its ownmethod3()
。This means that the realization ofInterface3
The class of an interface must implement all three methods. It is possible to useInterface3
interface to access all methods defined in its parent interface.
Multiple inheritance of interfaces is also very useful in software architecture as it allows for more flexible and reusable code. However, some programming languages do not allow multiple inheritance of interfaces, as this can lead to increased complexity and higher code maintenance costs. In these languages, other methods can be used to accomplish the same purpose, such as combining and delegating.
Why interfaces solve the multiple inheritance problem
First of all because not all subclasses have the same functionality, so the functions (methods) that are not encapsulated into an interface, and a class can implement multiple interfaces, so solves the problem of multiple inheritance in java
Difference between abstract classes and interfaces
Core difference. Abstract classes can contain common methods and common fields, which can be used directly by subclasses (no need to rewrite them), while interfaces can contain common methods and common fields.Cannot contain normal methods, subclasses must override all abstract methods.
Today’s sharing ends here, thank you for your support, it’s not easy to create, I hope you can give the blogger a trifecta!