Chapter 17 Java se special topic object-oriented advanced four - Interface

  • Interface business scenario: used to formulate standards, global constants and public abstract methods

1. Interface Overview

  • Interface: encapsulate some methods that are not implemented;

    • Subclass implementation interface: all abstract methods of the interface must be implemented
  • rule of grammar

interface Interface name{
    //1. Attributes
    //2. Static method
    //3. The default method requires the default keyword
    //4. Abstract method
}
  • example
//1. Create interface
interface Usb {
    public int AAA = 10;
    //1. Static method
    static void BBB(){
        System.out.println("Call static method...");
    }
    //2. Default method
    default public void CCC(){
        System.out.println("Call default method...");
    }
    //3. Abstract method 1
    public abstract void read();
    //4. Abstract method 2
    public abstract void write();
}

//2. Create a subclass of YouPan to implement the interface
public class YouPan implements Usb {
    public void read(){
        System.out.println("U Disk passing USB Read data...");
    }
    public void write(){
        System.out.println("U Disk passing USB Write data...");
    }
}

//3. Create JianPan subclass to implement the interface
public class JianPan implements Usb{
        public void read(){
            System.out.println("Keyboard passing USB Interface reading data...");
        }
        public void write(){
            System.out.println("Keyboard passing USB Interface write data...");
        }
}

//4. Create the main method and call two subclasses
public class UsbMain {
    public static void main(String[] args) {
        YouPan yp = new YouPan();
        yp.read();
        yp.write();

        JianPan jp = new JianPan();
        jp.read();
        jp.write();
    }
}
  • Operation results

2. Interface usage details

(1) The interface cannot be instantiated;

(2) All methods in the interface are public methods. Abstract methods in the interface can be modified without abstract;

public abstract void AAA();  Equivalent to void AAA()

(3) If a common class implements an interface, it must implement all abstract method implementations of the interface;

(4) The abstract class implements the interface, and all methods of the interface may not be implemented;

(5) Multiple interfaces can be implemented simultaneously;

calss A extends b,c{ }

(6) The attributes of the interface can only be final and are public static final modifiers;

(7) Attribute access in interface: interface name Attribute name

interface.attribute

(8) A class cannot inherit other classes, but can inherit multiple individual interfaces;

interface A extends B,C{ }

(9) Interface modifiers can only be public and default, which is the same as class modifiers

3. Difference between interface and inheritance

  • Interface is a supplement to java inheritance mechanism

(1) Interface and inheritance solve different problems

  • The value of inheritance: it can solve the reusability and maintainability of code;
  • Value of interface: design, design various specifications (Methods) and let other classes implement these methods;

(2) Interfaces are more flexible than inheritance

  • Inheritance is the relationship of is - a: cats are animals
  • The relationship between like - a when interfacing: cats are as cute as dogs

(3) The interface realizes code decoupling to a certain extent [i.e. interface normalization + dynamic binding]

  • example
//1. Create parent class - Monkey class
class Monkey{
    private String name;

    public Monkey(String name){
        this.name = name;
    }
    public  void climbing(){
        System.out.println("Monkeys can climb trees...");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

//2. Create interface 1 - Fishable
interface Fishable{
    void swimming();
}

//3. Create interface 2 - Birdable
interface Birdable{
    void flying();
}

//4. Create subclass - LittleMonkey inherits Monkey and implements fishable and birdable
class LittleMonkey extends Monkey implements Fishable,Birdable{
    public LittleMonkey(String name) {
        super(name);
    }

    public void swimming(){
        System.out.println(getName() + "Through study, you can swim like a fish...");
    }

    public void flying(){
        System.out.println(getName() + "Through learning, you can fly like a bird...");
    }
}

//5. Create the main method to implement the class LittleMonkey
public class Interface {
    public static void main(String[] args) {
        LittleMonkey lm = new LittleMonkey("Ignorance prayer");
        lm.climbing();
        lm.flying();
        lm.swimming();
    }
}
  • Operation results

4. The difference between interface and abstract class

Interfaceabstract class
There is no implementation code in the interfaceAbstract classes can have ordinary member methods, and variables can be defined
The method modifier in the interface can only be publicAbstract methods in abstract classes can have public, protected and default
There is no constructor in the interfaceThere can be construction methods
  • Interface and abstract class selection

(1) When we need a set of standardized methods, we can use the interface to implement the interface in the specific business, so as to meet the ever-changing and changeable requirements with unchanged. We only need to change the corresponding implementation class.
(2) If there is the same reusable code in multiple implementation classes, an abstract class can be added between the implementation class and the interface to extract the public code from the abstract class. Then, subclasses of different implementation processes are required to rewrite the methods in the abstract class to complete their respective businesses.

Keywords: Java intellij-idea

Added by davemwohio on Fri, 11 Feb 2022 18:27:43 +0200