Appearance
Interface
In abstract classes, abstract methods essentially define interface specifications: that is, stipulating the interface of high-level classes to ensure that all subclasses have the same interface implementation. In this way, polymorphism can exert its power.
If an abstract class has no fields, all methods are abstract methods:
java
abstract class Person {
public abstract void run();
public abstract String getName();
}
You can rewrite the abstract class into an interface: interface
.
In Java, use interface
to declare an interface:
java
interface Person {
void run();
String getName();
}
The so-called interface
is a pure abstract interface that is more abstract than an abstract class, because it cannot even have fields. Because all methods defined by the interface are public abstract
by default, these two modifiers do not need to be written (the effect is the same whether they are written or not).
When a specific class
implements an interface
, it needs to use the implements
keyword. For example:
java
class Student implements Person {
private String name;
public Student(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println(this.name + " run");
}
@Override
public String getName() {
return this.name;
}
}
We know that in Java, a class can only inherit from another class and cannot inherit from multiple classes. However, a class can implement multiple interface
, for example:
java
class Student implements Person, Hello { // Implemented two interfaces
...
}
The term
Pay attention to differentiating terms:
Java's interface specifically refers to the definition of interface , which represents an interface type and a set of method signatures, while programming interface generally refers to interface specifications, such as method signatures, data formats, network protocols, etc.
The comparison between abstract classes and interfaces is as follows:
abstract class | interface | |
---|---|---|
inherit | Can only extend one class | Can implement multiple interfaces |
Field | Instance fields can be defined | Cannot define instance fields |
abstract method | Abstract methods can be defined | Abstract methods can be defined |
non-abstract method | Non-abstract methods can be defined | Define the default method |
Interface inheritance
An interface
can inherit from another interface
. interface
inherits from interface
and uses extends
, which is equivalent to extending the methods of the interface. For example:
java
interface Hello {
void hello();
}
interface Person extends Hello {
void run();
String getName();
}
At this point, the Person
interface inherits from the Hello
interface, so Person
interface now actually has 3 abstract method signatures, one of which comes from the inherited Hello
interface.
Inheritance relationship
Properly design the inheritance relationship between interface
and abstract class
to fully reuse code. Generally speaking, public logic is suitable to be placed in abstract class
, specific logic is placed in each subclass, and the interface level represents the level of abstraction. You can refer to the inheritance relationship of a set of interfaces, abstract classes and concrete subclasses defined by Java's collection class:
┌───────────────┐
│ Iterable │
└───────────────┘
▲ ┌───────────────────┐
│ │ Object │
┌───────────────┐ └───────────────────┘
│ Collection │ ▲
└───────────────┘ │
▲ ▲ ┌───────────────────┐
│ └──────────│AbstractCollection │
┌───────────────┐ └───────────────────┘
│ List │ ▲
└───────────────┘ │
▲ ┌───────────────────┐
└──────────│ AbstractList │
└───────────────────┘
▲ ▲
│ │
│ │
┌────────────┐ ┌────────────┐
│ ArrayList │ │ LinkedList │
└────────────┘ └────────────┘
When used, the instantiated object can only be a specific subclass, but it is always referenced through the interface, because the interface is more abstract than the abstract class:
java
List list = new ArrayList(); // Use List interface to reference instances of concrete subclasses
Collection coll = list; // Upcast to Collection interface
Iterable it = coll; // Upcast to Iterable interface
Default method
In the interface, you can define default
methods. For example, change the run()
method of the Person
interface to the default
method:
java
public class Main {
public static void main(String[] args) {
Person p = new Student("Xiao Ming");
p.run();
}
}
interface Person {
String getName();
default void run() {
System.out.println(getName() + " run");
}
}
class Student implements Person {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}
Implementing classes do not need to override the default
method. The purpose of the default
method is that when we need to add a method to the interface, it will involve modifying all subclasses. If the new method is a default
method, then the subclass does not need to modify all the methods. It only needs to override the new method where it needs to be overridden.
The default
method is different from the ordinary method of abstract class. Because interface
has no fields, default
method cannot access the fields, but the ordinary methods of the abstract class can access the instance fields.
Summary
Java's interface defines a purely abstract specification, and a class can implement multiple interfaces;
Interfaces are also data types and are suitable for upward transformation and downward transformation;
All methods of the interface are abstract methods, and the interface cannot define instance fields;
Interfaces can define default
methods (JDK>=1.8).