Overview and characteristics of Java interface

Posted by JSHINER on Wed, 02 Feb 2022 02:35:26 +0100

After we understand inheritance, we all know the characteristics of inheritance.

1: Single inheritance (a son can only have one father)

2: Multi level inheritance (son inherits father, father inherits grandfather)

Therefore, the disadvantages of inheritance are obvious. The inheritance of Java language is single inheritance!!

Therefore, in order to solve the single inheritance problem, the Java language provides us with a mechanism to solve the problem - interface

Interface

An interface is a class that is more abstract than an abstract class. All methods in it are abstract methods and contain only constants and abstract methods. It can be called a collection of abstract methods and constant values

Keyword used by interface

The relationship between interface and class is implementation, not inheritance. This is because we have another keyword implements. The detailed usage will be described below

a key!!! Define the format of the interface class interface name{

[method body]

                ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​        ​​​​​​​    }

Next, let's use an example to help you understand (Bill Gates example)

Requirements: Bill Gates is both a programmer and a boss. As a programmer, he has the method of tapping code and as a boss, he has the method of management. Now what should we do to realize these two methods?

Analysis: from the demand, it is not difficult for us to think of creating a bill gates class, a programmer class and a boss class, so that he can have both the methods in the programmer class and the methods in the boss class. How do you do it? Inheritance? But there must be methods in two classes. Then we thought that we can inherit one class and then implement another class, that is, our interface.

The code is as follows

//main method class
public class jichengDemo {
	public static void main(String [] args){
        //create object
		BillGates gates =  new BillGates();
        //Knock code method
		gates.code();
        //management
		gates.manage();
		
	}
}
//Define boss class (as interface)
 interface Boss{
    //Abstract method!!
    //management
	public void manage();
}
//Define programmer class (as parent class)
class Programmer {
    //Knock code method
	public void code(){
		System.out.println("Knock code");
	}
}
//Define the Bill Gates class (as a subclass)
class BillGates extends Programmer implements Boss{
    //Override the abstract method of the interface class (must!!)
	public void manage(){
		System.out.println("Administration");
	}
}
//result
 Knock code
 Administration

This is what we need.

Of course, we can also define the programmer class as an interface, which is the multi implementation feature of the interface

The code is as follows

public class jichengDemo1 {
	public static void main(String [] args){
		BillGates gates =  new BillGates();
		gates.code();
		gates.manage();
		
	}
}
interface Boss{
	public void manage();
}
interface Programmer {
	public void code();
}
//Implement multiple interfaces
class BillGates implements Boss,Programmer{
	public void manage(){
		System.out.println("Administration");
	}
    public void code(){
        System.out.println("Knock code");    
    }
}

Multi implementation definition format {class BillGates Implementations Manager, programmer {[method body]}

Now that we have basically understood the use of interfaces, let's talk about the characteristics of interface members

① The methods in the interface can only be abstract methods

② The abstract method is decorated with the public abstract keyword by default (these two keywords can not be written, but it is strongly recommended to write them in the actual development process, otherwise it will feel cloudy)

③ The interface can only have constants, not variables

interface People{
    public static final int NUM = 10;
}

④ The constants in the interface are decorated with the public static final keyword by default

Because the static keyword is used, we can call the constant of People above with the class name

System.out.pritnln(People.NUM);

result

10

After learning the interface, we also need to understand the various relationships between the interface and the class

1: The relationship between classes (inheritance relationship) single inheritance and multi-layer inheritance

2: The relationship between interfaces (inheritance relationship) is multi inheritance, and there is no need to rewrite the abstract method of the parent interface. The specific readers write it by themselves. It will not be demonstrated here

//Multiple inheritance of interfaces
interface a{}
interface b{}
interface interc extends intera,interb{}

3: The relationship between interfaces and classes (implementation relationship) has multiple implementations (one son can have countless Godfathers)

Finally, let's talk about the advantages of the interface (welcome to add!!)

1: The multi implementation relationship between classes and interfaces breaks the limitations of inheritance

2: The interface provides external rules

3: It reduces the coupling of the program (modular development can be realized, rules can be defined, and everyone can implement their own modules to improve the development efficiency)

I hope readers can be inspired by this article. I hope it can help you. Thank you!!! / manual dog head emoji

Topics: Java Back-end interface