We know that only single inheritance is supported in Java, but if we want to define some functions and want a subclass to inherit and implement, obviously there is no way to do it. All Java provides the concept of interfaces.In this way, we can use one subclass to implement multiple interfaces. We can understand that the interface is a special abstract class. In order to reflect the scalability of things, Java provides interfaces to define these additional functions, and does not give specific implementations. In the future, which classes need to inherit these functions, only need to implement the interface.
Objects cannot be created in the interface, complete instantiation
The interface is different from the class, the interface does not have an overview of the constructor
The member variables in the interface are all public static constants, and there is a default public static final modifier before the member variables
There are no non-abstract methods in the interface, all are abstract methods, and the default modifier public abstract modifier before the method
publicclassMyTest{
publicstaticvoidmain(String[] args){
//接口中的公共静态常量,可以直接通过接口名直接调用
System.out.println(MyInterface.num);}}/*接口:interface它主要定义一些扩展的功能,将来
哪些事物想要具备这些扩展功能,就可以实现这个接口
接口,其实用来定义一些规范,对于这些规范的实现,
肯定由子类来进行具体的实现
*/interfaceMyInterface{
//接口中的成员变量默认为公共的静态常量//成员变量前存在public static final的修饰符publicstatic final int num=30;//接口中的方法全部都是抽象方法//方法前存在默认public abstract修饰符public abstract voidaa();}
3. The relationship between classes and interfaces
The relationship between classes and interfaces
The relationship between the class and the class: the inheritance relationship is formed between the class and the class, and can only be single inheritance, and multi-layer inheritance
The relationship between the class and the interface: the relationship between the class and the interface is the relationship of realization, and can be implemented more (that is, a class can implement multiple interfaces)
The relationship between the interface and the interface: the relationship between the interface and the interface is an inheritance relationship, and multiple inheritance is possible
3.1 The relationship between classes
The relationship between the class and the class: the inheritance relationship is formed between the class and the class, and can only be single inheritance, and multi-layer inheritance
publicclassMyTest{
publicstaticvoidmain(String[] args){
//多态:父类的引用指向子类的对象
Fu fu =newZi();//多态访问成员变量,编译看父类,运行看父类
System.out.println(fu.num);
fu.show();//想要访问子类中特有的成员需要向下转型
Zi zi=(Zi) fu;
System.out.println(zi.num);}}//父类classFu{
//父类的成员变量
int num=30;publicvoidshow(){
System.out.println("父类的show方法");}}//子类classZiextendsFu{
//子类的成员变量
int num=100;//子类重写父类的成员方法
@Override
publicvoidshow(){
System.out.println("子类的成员方法");}}
3.2 The relationship between classes and interfaces
The relationship between the class and the interface: the relationship between the class and the interface is the relationship of realization, and can be implemented more (that is, a class can implement multiple interfaces)
//接口,接口中的方法都是抽象方法,//方法前有public abstract的默认修饰符interfaceA{
public abstract voida();public abstract voidaa();}interfaceB{
public abstract voidb();}//类与接口之间的关系:类与接口的关系是实现关系//也就是说一个类可以实现多个接口classCimplementsA,B{
@Override
publicvoidaa(){
}
@Override
publicvoida(){
}
@Override
publicvoidb(){
}}
3.3 The relationship between interface and interface
The relationship between interface and interface: the relationship between interface and interface is inheritance relationship, and can carry out multiple inheritance
interfaceA{
public abstract voida();}interfaceB{
public abstract voidb();}//接口与接口之间的关系:是继承的关系,并且可以多继承interfaceCextendsA,B{
public abstract voidc();}
4. Precautions in the interface
Notes in the interface
Before JDK1.8, the interface was all abstract methods; after JDK1.8, some changes were made to the interface, and the method in the interface can be given a specific implementation (this method can be a non-abstract method), and there must be a default modifier before the method
After JDK1.8, static methods can be defined in the interface
publicclassMyTest{
publicstaticvoidmain(String[] args){
C c =newC();
c.aa();
c.bb();A.show();}}interfaceA{
//接口中,JDK1.8后可以定义静态方法publicstaticvoidshow(){
System.out.println("这是一个静态方法");}//接口中抽象方法public abstract voida();/*JDK1.8后,接口中的方法可以是非抽象方法
但方法前必须加上default修饰符
*/publicdefaultvoidaa(){
System.out.println("这是一个非抽象方法");}}interfaceB{
public abstract voidb();publicdefaultvoidbb(){
System.out.println("这是一个非抽象方法11");}}classCimplementsA,B{
@Override
publicvoida(){
System.out.println("对接口中的抽象方法进行重写");}
@Override
publicvoidb(){
System.out.println("对接口中的抽象方法进行重写11");}}
5. The difference between abstract class and interface
The difference between abstract class and interface is:
Membership difference
category
The difference between member variables
Whether there is a construction method
Member method
Abstract class
It can be a variable or a constant
Constructor in abstract class
There can be abstract methods or non-abstract methods
interface
All are public static constants
Interface has no concept of constructor
The member methods are only abstract methods (after JDK1.8, you can define default modified methods, you can give the specific implementation of the function, and subclasses can inherit and use; after JDK1.8, you can also define static methods in the interface.
Relationship difference:
The relationship between classes and interfaces
The relationship between class and class: inheritance relationship, the inheritance relationship is single inheritance, not multiple inheritance, multiple inheritance
The relationship between the class and the interface: the realization relationship, can be single realization, can be multiple realization (a class can realize multiple interfaces)
The relationship between interface and interface: inheritance relationship, single inheritance, multiple inheritance
The difference in design concept:
The difference in design concept
Abstract class: The abstract class defines the common functions in the inheritance system
Interface: What is defined in the interface is the extended function of the inheritance system