Interface Overview (understanding)

Posted by plsanders on Sat, 25 Dec 2021 07:05:48 +0100

  • Interface is a public standard. As long as it conforms to the standard, everyone can use it.

  • Two meanings of interface in Java

    1. Used to define specifications

    2. Used to expand functions

    3. The interface is decorated with the keyword interface

      public interface Interface name {} 

      The class implementation interface is represented by implements

    4. public class Class name implements Interface name {}

    5. Interface cannot be instantiated

      We can create the implementation class object of the interface

    6. Subclass of interface

      Or override all abstract methods in the interface

      Either subclasses are abstract classes

    7. Member characteristics of the interface (memory)

    8. Member characteristics

      • Member variable

        Can only be constants. Default modifier: public static final

      • Construction method

        No, because the interface mainly extends functions, but does not exist

      • Member method

        Can only be abstract methods

        Default modifier: public abstract

        As for the methods in the interface, JDK8 and JDK9 have some new features, which will be explained later

    9. Code demonstration

      • Interface

      • public interface Inter {
            public static final int NUM = 10;
        
            public abstract void show();
        }

        Implementation class

      • class InterImpl implements Inter{
        
            public void method(){
                // NUM = 20;
                System.out.println(NUM);
            }
        
            public void show(){
        
            }
        }

        Test class

      • public class TestInterface {
            /*
                Member variable: can only be constant. Three keywords will be added by default
                            public static final
                Construction method: None
                Member methods: can only be abstract methods. The system will add two keywords by default
                            public abstract
             */
            public static void main(String[] args) {
                System.out.println(Inter.NUM);
            }
          
        }

        Relationship between class and interface (memory)

      • Relationship between classes

        Inheritance relationship can only be inherited in single layer, but it can be inherited in multiple layers

      • Relationship between class and interface

        The implementation relationship can be implemented alone or multiple, and multiple interfaces can be implemented while inheriting a class

      • Relationship between interfaces

        Inheritance relationship can be single inheritance or multiple inheritance

      • Default method in interface [apply]

      • format

        public default return value type method name (parameter list) {}

      • effect

        Solve the problem of interface upgrade

      • example

      • public default void show3() { 
        }

        matters needing attention

      • The default method is not an abstract method, so it is not forced to be overridden. But it can be rewritten. When rewriting, remove the default keyword

      • public can be omitted, but default cannot be omitted

      • If multiple interfaces are implemented and the same method declaration exists in multiple interfaces, the subclass must override the method

      • Static method in interface [ application ]

      • format

        public static return value type method name (parameter list) {}

      • example

      • public static void show() {
        }

        matters needing attention

      • Static methods can only be called through the interface name, not through the implementation class name or object name

      • public can be omitted, but static cannot be omitted

      • Private method in interface [application]

      • Causes of private methods

        The private method with method body is added in Java 9, which actually lays the groundwork in Java 8: Java 8 allows the default method and static method with method body to be defined in the interface. This may lead to a problem: when two default methods or static methods contain the same code implementation, the program must consider extracting this implementation code into a common method, and this common method does not need to be used by others. Therefore, it is necessary to hide it with private. This is the necessity of adding private methods in Java 9

      • Define format

        • Format 1

          private Return value type method name(parameter list) { }

        • Format 2

          private static Return value type method name(parameter list) { }

        • matters needing attention

        • The default method can call private static and non static methods

        • Static methods can only call private static methods

        • Overview of polymorphism (memory)

        • What is polymorphism

          The same object shows different forms at different times

        • Polymorphic premise

          • There should be inheritance or implementation relationship

          • There should be method rewriting

          • A parent class reference should point to a child class object

        • Code demonstration

        • class Animal {
              public void eat(){
                  System.out.println("Animals eat");
              }
          }
          
          class Cat extends Animal {
              @Override
              public void eat() {
                  System.out.println("Cats eat fish");
              }
          }
          
          public class Test1Polymorphic {
              /*
                  Prerequisites for polymorphism:
          
                      1. There should be (inheritance \ Implementation) relationship
                      2. There must be a way to rewrite
                      3. To have a parent class reference, point to a child class object
               */
              public static void main(String[] args) {
                  // The current thing is a cat
                  Cat c = new Cat();
                  // The current thing is an animal
                  Animal a = new Cat();
                  a.eat();
          
              }
          }

          Basic use of internal classes (understanding)

        • Inner class concept

          • Define a class in a class. For example, if a class B is defined inside a Class A, class B is called an inner class

        • Internal class definition format

          • Format & ex amp le:

          • /*
            	Format:
                class External class name{
                	Modifier class internal class name{
                	
                	}
                }
            */
            
            class Outer {
                public class Inner {
                    
                }
            }

            Access characteristics of internal classes

          • Internal classes can directly access members of external classes, including private classes

          • An object must be created for an external class to access members of an internal class

          • /*
                Internal class access features:
                    Internal classes can directly access members of external classes, including private classes
                    An object must be created for an external class to access members of an internal class
             */
            public class Outer {
                private int num = 10;
                public class Inner {
                    public void show() {
                        System.out.println(num);
                    }
                }
                public void method() {
                    Inner i = new Inner();
                    i.show();
                }
            }

          • The definition location of the class inside the member

            • In a class, a method is in the same place as a member variable

          • External member internal class format

            • Format: external class name Internal class name object name = external class object Internal class objects;

            • Example: outer Inner oi = new Outer(). new Inner();

          • Private member inner class

            • For the purpose of designing a class as an internal class, most of them do not want to be accessed by the outside world, so the definition of the internal class should be privatized. After privatization, a method that can be called by the outside world is provided. The internal class object is created and called inside the method.

            • Example code:

            • class Outer {
                  private int num = 10;
                  private class Inner {
                      public void show() {
                          System.out.println(num);
                      }
                  }
                  public void method() {
                      Inner i = new Inner();
                      i.show();
                  }
              }
              public class InnerDemo {
                  public static void main(String[] args) {
              		//Outer.Inner oi = new Outer().new Inner();
              		//oi.show();
                      Outer o = new Outer();
                      o.method();
                  }
              }

              Static member inner class

            • Static member internal class access format: external class name Internal class name object name = new external class name Internal class name ();

            • Static method in inner class of static member: external class name Internal class name Method name ();

            • Sample code

            • class Outer {
                  static class Inner {
                      public void show(){
                          System.out.println("inner..show");
                      }
              
                      public static void method(){
                          System.out.println("inner..method");
                      }
                  }
              }
              
              public class Test3Innerclass {
                  /*
                      Static member inner class demo
                   */
                  public static void main(String[] args) {
                      // External class name Internal class name object name = new external class name Internal class name ();
                      Outer.Inner oi = new Outer.Inner();
                      oi.show();
              
                      Outer.Inner.method();
                  }
              }

              Local inner class (understanding)

            • Local internal class definition location

              • A local inner class is a class defined in a method

            • Local internal class mode

              • Local internal classes cannot be used directly by the outside world. You need to create objects inside the method and use them

              • This class can directly access members of external classes or local variables in methods

            • Sample code

              class Outer {
                  private int num = 10;
                  public void method() {
                      int num2 = 20;
                      class Inner {
                          public void show() {
                              System.out.println(num);
                              System.out.println(num2);
                          }
                      }
                      Inner i = new Inner();
                      i.show();
                  }
              }
              public class OuterDemo {
                  public static void main(String[] args) {
                      Outer o = new Outer();
                      o.method();
                  }
              }
              

              Anonymous inner class (application)

            • Premise of anonymous inner class

              • There is a class or interface, where the class can be concrete or abstract

            • Format of anonymous inner class

              • Format: new class name () {rewrite method} new interface name () {rewrite method}

              • give an example:

              • new Inter(){
                    @Override
                    public void method(){}
                } 

              • The nature of anonymous inner classes

                • Essence: it is an anonymous object that inherits the class or implements the subclass of the interface

              • Details of anonymous inner classes

                • Anonymous inner classes can be accepted in the form of polymorphism

                • Inter i = new Inter(){
                    @Override
                      public void method(){
                          
                      }
                  }

                  Anonymous inner classes call methods directly

                • interface Inter{
                      void method();
                  }
                  
                  class Test{
                      public static void main(String[] args){
                          new Inter(){
                              @Override
                              public void method(){
                                  System.out.println("I am an anonymous inner class");
                              }
                          }.method();	// Direct call method
                      }
                  }

Topics: Java Back-end