JAVASE Xiaobai study notes (10) into the object-oriented interface

1. The introduction of the interface

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.

public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        //接口不能直接创建对象
        //子类和接口之间的关系是实现关系,implements
        //可以将子类与接口之间的关系称为:父接口,子类
        MyInterface myInterface=new Cat();
        myInterface.jump();
    }
}

/*interface定义一个接口,接口中定义的都是一些扩展的功能
  哪类事物想要具备该扩展功能,需要实现该接口*/
interface  MyInterface{
    
    
    //接口中定义了扩展的功能:跳高
    public abstract void jump();
}

//抽象类
abstract class Animal{
    
    
    //抽象方法
    public abstract void eat();
    public abstract void sleep();
}

//类与接口的关系是实现的关系
//接口中的抽象方法,实现的类必须对方法重写
class Cat extends Animal implements MyInterface{
    
    
    @Override
    public void eat() {
    
    
        System.out.println("小猫吃鱼");
    }

    @Override
    public void sleep() {
    
    
        System.out.println("小猫喜欢在温暖的地方睡觉");
    }

    @Override
    public void jump() {
    
    
        System.out.println("小猫经过学习学会了跳高");
    }
}

输出的结果为:
小猫经过学习学会了跳高


2. Features of members in the interface

Features of members in 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
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        //接口中的公共静态常量,可以直接通过接口名直接调用
        System.out.println(MyInterface.num);

    }
}
/*接口:interface它主要定义一些扩展的功能,将来
  哪些事物想要具备这些扩展功能,就可以实现这个接口
  接口,其实用来定义一些规范,对于这些规范的实现,
  肯定由子类来进行具体的实现
 */
interface MyInterface{
    
    
    //接口中的成员变量默认为公共的静态常量
    //成员变量前存在public static final的修饰符
  public static final  int num=30;
    //接口中的方法全部都是抽象方法
    //方法前存在默认public abstract修饰符
  public abstract void aa();
}

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

public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        //多态:父类的引用指向子类的对象
        Fu fu = new Zi();
        //多态访问成员变量,编译看父类,运行看父类
        System.out.println(fu.num);
        fu.show();
        //想要访问子类中特有的成员需要向下转型
        Zi zi= (Zi) fu;
        System.out.println(zi.num);
    }

}
//父类
class Fu{
    
    
    //父类的成员变量
    int num=30;
    public void show(){
    
    
        System.out.println("父类的show方法");
    }
}

//子类
class Zi extends Fu{
    
    
    //子类的成员变量
    int num=100;
    //子类重写父类的成员方法

    @Override
    public void show() {
    
    
        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的默认修饰符
interface A{
    
    
 public abstract void a();
 public abstract void aa();
}

interface B{
    
    
    public abstract void b();
}
//类与接口之间的关系:类与接口的关系是实现关系
//也就是说一个类可以实现多个接口
class C implements  A,B
{
    
    
    @Override
    public void aa() {
    
    

    }
    @Override
    public void a() {
    
    

    }
    @Override
    public void b() {
    
    

    }
}

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

interface A{
    
    
    public abstract void a();
}

interface B{
    
    
    public abstract void b();
}

//接口与接口之间的关系:是继承的关系,并且可以多继承
interface C  extends A,B{
    
    
    public abstract void c();
}

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
public class MyTest {
    
    
    public static void main(String[] args) {
    
    
        C c = new C();
        c.aa();
        c.bb();
        A.show();
    }
}

interface A{
    
    
    //接口中,JDK1.8后可以定义静态方法
    public static void show(){
    
    
        System.out.println("这是一个静态方法");
    }
    //接口中抽象方法
    public abstract void a();
    /*JDK1.8后,接口中的方法可以是非抽象方法
      但方法前必须加上default修饰符
     */
    public default void aa(){
    
    
        System.out.println("这是一个非抽象方法");
    }
}

interface B{
    
    
    public abstract void b();
    public default void bb(){
    
    
        System.out.println("这是一个非抽象方法11");
    }
}

class C implements A,B{
    
    
    @Override
    public void a() {
    
    
        System.out.println("对接口中的抽象方法进行重写");
    }

    @Override
    public void b() {
    
    
        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

Guess you like

Origin blog.csdn.net/qq_41537102/article/details/110594212