In depth analysis of the use of interfaces in Java programming

  • 2020-04-01 04:19:33
  • OfStack

The essence of an interface - an interface is a special kind of abstract class that contains only definitions of constants and methods, but no implementations of variables and methods.

Abstract class have something interface can have, if an abstract class inside all of the methods are all abstract, there is no any way to this abstract class to be realized, and the abstract class inside all the variables are static (static), is can't change the variable (final), then such an abstract class can be defined an interface (interface). The format for defining a class as an interface is to replace the class keyword with the interface keyword.

Interface (interface) is a special kind of abstract class, in this kind of abstract classes, all methods are abstract methods, and the properties of this abstract class (member variables) are declared as "public static final type attribute name", the default is also a statement as "public static final" namely inside member variables are public, static, cannot change. Therefore, when declaring constants in the interface, it can be written as "public static final type constant name =value(value)" or "type constant name =value(value)" as follows: "Public static final int id=10" can be directly written as "int id=10", because the default property declaration in the interface is "public static final", so "public static final" can be omitted. Abstract methods declared in the interface can not write the abstract keyword to identify, because all methods in the interface are abstract, so the "abstract" keyword is omitted by default, such as in an interface declaration of such three methods: "Public void the start ()", "public void the run ()", "public void the stop ()" in front of the three methods are not using the abstract keyword to identify, but they just abstract method, because in the inside of the interface methods declared abstract method, therefore in the interface of abstract method saves the abstract keyword is omitted, because the default statement method is abstract, so there is no need to write the word "abstract", This is different from declaring an abstract method in an abstract class, where the abstract method must use the "abstract" keyword, and in an interface where the abstract method can be omitted. Note: inside the interface declared abstract method is the default "public (public)", is "the reason why public (public)" this statement is to fix problems more easily when multiple inheritance in c + +, c + + multiple inheritance is easy to appear problem, the problem is that multiple inheritance between multiple parent class if they have the same member variables, it would be quite troublesome, this reference and run will produce all sorts of problems. In order to fix this problem, JAVA changes all the member variables in the interface into static final, and the member variable is of static type, so this member variable belongs to the whole class, rather than to a specific object. For multiple inheritance, there are actually multiple parent objects in a subclass object, whereas for single inheritance, there is only one parent object in a subclass object. Multiple inherited subclass objects have multiple parent class objects, and there may be duplicate member variables between these parent class objects, which is very easy to cause problems. Therefore, this problem is avoided in JAVA, and the interface is adopted to achieve multiple inheritance. As a class interface, can from the interface inheritance (or interface), which is multiple inheritance, the inside of the interface member variables not to belong to an object, is a static member variables, belongs to the whole class, so a class to implement multiple interfaces also doesn't matter, does not exist the problem of conflict between objects. Implementing multiple interfaces enables multiple inheritance without the pitfalls of multiple inheritance, which is the benefit of using interfaces for multiple inheritance.

1. Define the interface
      Use interface to define an interface. Interface definition is similar to the definition of the same kind, is also divided into interface declaration and interface body, which is composed of constant definition and method definition. The basic format of the interface definition is as follows:
              Interface name [extends parent interface list]{
                              [public] [static] [final] constant;
                              [public] [abstract] method;
              }
Modifier: optional to specify access to the interface, with an optional value of public. If omitted, the default access is used.
Interface name: a required parameter to specify the name of the interface, which must be a valid Java identifier. In general, you want to capitalize the first letter.
Extends: list of parent interface names: an optional parameter that specifies which parent the interface to be defined inherits from. When using extends key
                          The parent interface is named as a required parameter.
Methods: methods in an interface are defined but not implemented.
For example, define an interface for computation, in which a constant PI and two methods are defined, as follows:


public interface CalInterface  
{ 
  final float PI=3.14159f;//Defines the constant PI used to represent PI
  float getArea(float r);//Define a method getArea() to calculate the area
  float getCircumference(float r);//Define a method getCircumference() to calculate the circumference
} 

Note:
      As with Java class files, the filename of the interface file must be the same as the interface name.
2. Implement the interface
After the interface is defined, it can be implemented in the class. Implementing an interface in a class can use the keyword implements, in the following basic format:
              [modifier] class < The name of the class > [extends parent name] [implements interface list]{
                    .
              }
Modifier: optional parameter that specifies the class's access rights, with optional values public, abstract, and final.
Class name: required parameter to specify the name of the class, which must be a valid Java identifier. In general, you want to capitalize the first letter.
Extends parent name: an optional parameter that specifies which parent the class to be defined inherits from. When using the extends keyword, the parent class is called must
Optional parameters.
Implements interface list: an optional parameter that specifies which interfaces the class implements. When you use the implements keyword, the list of interfaces is a required parameter. When there are more than one interface name in the list of interfaces, the interface names are separated by commas.
      When implementing an interface in a class, the name of the method, the type of return value, the number of arguments, and the type must be exactly the same as in the interface, and all methods in the interface must be implemented. For example, write a class named Cire that implements the interface Calculate defined in section 5.7.1, as follows:


public class Cire implements CalInterface  
{ 
  public float getArea(float r)  
  { 
    float area=PI*r*r;//Calculate the area of the circle and assign a value to the variable area
    return area;//Returns the calculated area of the circle
  } 
  public float getCircumference(float r)  
  { 
    float circumference=2*PI*r;   //Calculate the circumference and assign the value to the variable circumference
    return circumference;      //Returns the calculated circumference
  } 
  public static void main(String[] args)  
  { 
    Cire c = new Cire(); 
    float f = c.getArea(2.0f); 
    System.out.println(Float.toString(f)); 
  } 
} 

          In class inheritance, you can only do single reinheritance, but when implementing an interface, you can implement multiple interfaces at once, separated by the comma ", ".
          In this case, constant or method name conflicts may occur. To solve this problem, if constant conflicts occur, the interface of the constant needs to be explicitly specified, which can be achieved by "interface name.constant". If a method conflict occurs, just implement one method.


Related articles: