Java object oriented interface

Time:2021-3-12

Definition and implementation of interface

 

  1. First knowledge:

    1. Common class: only concrete implementation, the keyword of declaration class is class

    2. Abstract class: there are concrete implementation and specification (abstract method). The keyword of declaring abstract class is abstract

    3. Interface: only specification, the key to declare interface is interface

    I can’t write method ~ professional constraint! The work is more about separation of constraints and Implementation: interface oriented programming~

  2. understand:
    1. Interface is a specification, which defines a set of rules, reflecting the idea of “if you are…, you must be able to…” in the real world.

    If you are an airplane, you must be able to fly; if you are a car, you must be able to run.

    2. The essence of interface is contract, just like our law, which everyone abides by after being made.

    3. The essence of OO (object oriented) is the abstraction of objects, and the interface is the best embodiment of this.

    Why we discuss design patterns only for languages with abstract ability (such as C + +, Java, C #), is because what design patterns study is actually how to reasonably abstract.

  3. Note: all interfaces need to have implementation classes

     

User business layer interface

 

  1. Interface keyword

    The constant defined in the interface is public static final by default and can not be written, for example: public static final int age = 99;

    The method defined in the interface is public abstract by default and can not be written, such as public abstract void add (string name);

    Generally, we define some methods in the interface, but we rarely define constants in the interface

  2. Interface instance:
    package oop.demo13;
    
    /**
     *Definition and implementation of Java object oriented interface
     *
     *First knowledge:
     *1. Common class: only concrete implementation, the keyword of declaration class is class
     *2. Abstract class: there are concrete implementation and specification (abstract method), and the keyword of declaring abstract class is abstract
     *3. Interface: only specification, the keyword of the declared interface is interface, and it can't write method ~ professional constraint! The work is more about separation of constraints and Implementation: interface oriented programming~
     *
     *Understanding:
     *1. Interface is a specification, which defines a set of rules, reflecting the idea of "if you are..., you must be able to..." in the real world.
     *If you are an airplane, you must be able to fly; if you are a car, you must be able to run.
     *2. The essence of interface is contract. Just like our laws, we will abide by them after they are formulated.
     *3. The essence of OO (object oriented) is the abstraction of objects. The interface is the best embodiment of this.
     *Why we discuss design patterns only for languages with abstract ability (such as C + +, Java, C #), is because what design patterns study is actually how to reasonably abstract.
     *
     *Note: all interfaces need to have implementation classes
     */
    Public interface userservice {// user business layer
    
        /**
         *The constant defined in the interface is public static final by default and can not be written,
         *For example: public static final int age = 99;
         *Generally, we define some methods in the interface, but we rarely define constants in the interface
         */
        int AGE = 99;
    
        /**
         *All definitions in an interface are abstract
         *The method defined in the interface is public abstract by default, and can not be written,
         *For example, public abstract void add (string name);
         */
        void add(String name);
    
        void delete(String name);
    
        void update(String name);
    
        void query(String name);
    }

Time service layer interface

 

package oop.demo13;

/**
 *Time business layer
 */
public interface TimeService {

    //Timing
    void timer();
}

Interface implementation class

 

  1. Implements keyword

    A class implements the interface through the implements keyword. To implement the interface, it is necessary to rewrite the methods in the interface

    Using interfaces to implement multiple inheritance: a class can implement multiple interfaces, just rewrite the methods in the interface

    Note: to implement an interface class, you need to override the methods in the interface~

    Shortcut: Alt + insert – > override methods (Ctrl + O)

  2. Interface implementation class instance:
    package oop.demo13;
    
    /**
     *Abstract class: Extensions~
     *A class implements the interface through the implements keyword. To implement the interface, it is necessary to rewrite the methods in the interface
     *To implement an interface class, you need to override the methods in the interface~
     *Using interfaces to implement multiple inheritance: a class can implement multiple interfaces, just rewrite the methods in the interface
     *Shortcut: Alt + insert - > override methods (Ctrl + O)
     */
    public class UserServiceImpl implements UserService, TimeService {
        @Override
        public void add(String name) {
    
        }
    
        @Override
        public void delete(String name) {
    
        }
    
        @Override
        public void update(String name) {
    
        }
    
        @Override
        public void query(String name) {
    
        }
    
        @Override
        public void timer() {
    
        }
    }

Interface summary

 

Abstract thought:

  •  The interface acts as a constraint
  •  Define some methods for different people to implement. For example, 10 people implement an interface, but there are 10 different implementation methods
  •  The default method in the interface is public abstract
  •  Default constant in interface: public static final
  •  Interfaces, like abstract classes, cannot be instantiated ~ there are no construction methods in interfaces
  •  Implements can implement multiple interfaces
  •  To implement an interface, you must override the methods in the interface