Introduction to Java: Interfaces and Polymorphism

interface

A reference data type, the common specification of multiple classes
will still generate .class after compilation
(the same kind of IDEA was created, select interface)

public interface 接口名{
    
    
常量
抽象方法
默认方法
静态方法
私有方法}

Interface use: there must be an implementation class, create an implementation class object for use

public class 实现类名称 implements 接口名称{
    
    
//必须覆盖接口中所有抽象方法}

If the implementation class does not cover all abstract methods in the rewrite interface, then the implementation class must be an abstract class

Abstract method abstract :
the abstract method in the interface, the modifier must be public abstract (can be omitted)

The default method default :
In order to solve the problem of interface upgrade (add new abstract methods to the interface) the
default method can also be overridden by the implementation class

public default 返回值类型 方法名称(参数){
    
    };

The default method modifier must be public (can be omitted) to
call the default method, if there is no implementation class, it will look for the interface

Static method static :

public static 返回值类型 方法名称(参数){
    
    };

The default method modifier must be public (can be omitted)
static methods cannot be called through objects of the interface implementation class! Call directly
through the interface name (static has nothing to do with the object)

Private method private : It is
necessary to extract a public method to solve the problem of code duplication in the two methods, but it must be privatized.
Ordinary private method, static private method (to solve the code duplication of static method)

private 返回值类型 方法名称(参数){
    
    };
private static 返回值类型 方法名称(参数){
    
    };

Ordinary private methods are called through objects, and static private methods are called directly through interfaces

Member variables can also be defined in the interface, which must be modified with public static final , and must be initialized and assigned (equivalent to constants ) (can be omitted, but the nature remains the same) (usually all capitals are used to represent constants)

public interface 接口名{
    
    
	public static final 数据类型 变量名 = 初始值;}

(The final keyword stands for unchangeable )
 

A class can implement multiple interfaces at the same time

public class 实现类名 implements 接口名1,接口名2{
    
    }

When there are repeated abstract methods in multiple interfaces, you only need to override and rewrite once.
When there are repeated default abstract methods in multiple interfaces , you must uniformly override the conflicting default methods and override
the default and interface in the parent class. The default method is repeated, and the method of the parent class is preferred
. Inheritance in Java is preferred to interface implementation

public class 类名 extends 父类 implements 接口{
    
    };

Classes and classes are single-inherited (a class has only one direct parent).
Classes and interfaces are multi-implemented (a class can implement multiple interfaces).
Interfaces and interfaces are multi-inherited (an interface can have multiple parent interfaces)
( The abstract methods in multiple parent interfaces can be repeated, and the default methods in multiple parent interfaces cannot conflict -> subinterfaces must be overwritten)


Polymorphism

The premise of polymorphism is inheritance (extends+implements)
an object has multiple forms at the same time: parent class references point to subclass objects
(left parent and right child, treat the object as a larger range of things)

父类 对象名 = new 子类();
接口 对象名 = new 实现类();

When the parent class and subclass member variable has the same name, whose data type (left side of the equal sign) is used when assigning the member variable. When the
parent class and subclass member method has the same name, who is new when assigning the value (right side of the equal sign) Who it is is to use whose membership method

The advantage of polymorphism:
no matter which subclass of new is, the calling method of the parent class on the left will not change

Guess you like

Origin blog.csdn.net/Rachel_IS/article/details/104459875