Java interfaces

Time:2023-10-7

Java interfaces

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!

The concept of interfaces

In real life, examples of interfaces abound, e.g., USB ports on laptops, power outlets, etc.
Java interfaces
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:

  1. 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.

  2. Interface definitions use the keywordinterface

  3. Method declarations in interfaces do not require specific implementations, only method names, parameter lists, and return value types need to be defined.

  4. Method names follow the naming rules for identifiers and must begin with a capital letter.

  5. Methods in interfaces are not allowed to contain code blocks, and constructor methods.

  6. Methods in interfaces can have parameter lists of basic data types, class types, interface types, and array types.

  7. Interfaces can inherit from multiple interfaces, using the extends keyword.

  8. A class can implement multiple interfaces, using the implements keyword.

  9. A class that implements an interface must implement all the methods in the interface.

  10. Methods in interfaces are public by default, but you cannot use access modifiers to modify their access.

Interface Characteristics

Java interfaces

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 interfaceInterface3inheritedInterface1andInterface2method, and added a method of its ownmethod3()This means that the realization ofInterface3The class of an interface must implement all three methods. It is possible to useInterface3interface 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.
Java interfaces

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!Java interfaces

Recommended Today

uniapp and applet set tabBar and show and hide tabBar

(1) Set the tabBar: uni.setTabberItem({}); wx.setTabberItem({}); indexnumberisWhich item of the tabBar, counting from the left, is indexed from 0.textstringnoButton text on tabiconPathstringnoImage PathselectedIconPathstringnoImage path when selectedpagePathstringnoPage absolute pathvisiblebooleannotab Whether to display uni.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) wx.setTabBarItem({ index: 0, text: ‘text’, iconPath: ‘/path/to/iconPath’, selectedIconPath: ‘/path/to/selectedIconPath’, pagePath: ‘pages/home/home’ }) […]