Review the interface on the second day

Access characteristics of construction method under inheritance relationship

Purifying Mantra: Subclass objects should be loaded first to load parent class objects; //Subclass objects should be loaded first to load parent class -> also to the 
        
    little secret of the construction method: 
        1. The first line of code inside each construction method hides a sentence by default : super(); 
        2. Calling other constructors in the constructor This code must be the first valid code of the constructor 
        3. super (actual parameter): call the parameter-containing constructor of the parent class 
        4. If you are constructing If any other construction method is called in the first sentence of the method, the super() given by default will not be given away. 5. 
        It is not allowed to call other construction methods twice 
        in a construction method. 7. 
        The construction method in the subclass must have a construction that is used to load the parent class object     

Use of this and super

        this: The reference of the object of this class 
        this. Member variable: Break the constraint of the local position, and force access to the variable at the member position of this class 
        this. Member method (): -> Meaningless all methods are member methods 
            every time this is called The method in the class actually hides this in front of it. 
        this() : call the no-argument construction method of this class 
        this(actual parameter): call the argument-containing construction method of this class ​super: the reference 
        super 
of
        the parent class object of this class . Member variables: Break the constraints of the position of this class, and force access to variables at the member positions of the parent class of this class 
        super.Member method (): Break the constraints of the position of this class, and force the member method of the parent class of this class to be called 
        super(): Call this No-argument construction super(actual parameter) of class parent class 
        : call parent class construction with parameters

Advantages and Disadvantages of Inheritance

The benefits of inheritance: 
    1. Extract the commonality (common attributes and common behaviors) of the subclasses into the parent class -> improve the reusability of the code // Look from the subclass to the parent class 2. The parent class constrains the 
    subclass Behavior//Looking from the parent class to the child class 
        
Disadvantages of inheritance: 
    high cohesion and low coupling 
        Cohesion: the ability to complete functions independently 
        Coupling: dependency 
    When inheritance is needed, inherit decisively, and the advantages of inheritance outweigh the disadvantages! 00

Explanation of the question types of inherited knowledge points

abstract class

Abstract class: A class that is more abstract than a class is called an abstract class 
    Reason: There may be abstract methods in an abstract class 
    Why does an abstract class exist: Strictly restrict the behavior of subclasses 
​abstract
    : abstract adj. 
        Can be used to modify classes: class changes abstract classes 
        can Used to modify the method: the method becomes an abstract method 
    
Format: 
    public abstract class class name{ 
    ​}

abstract method

Abstract method: method without method body 
format
: 
    public abstract return value type method name (formal parameter list); 
function
: 
    constrain the behavior of subclasses and require subclasses to implement abstract methods!! 
            (or subclasses themselves become abstract classes Abstract methods can only be in abstract classes or interfaces)

member of abstract class

        //Custom constant: can have, or can have multiple 
        1. Member variable: can have, or can have multiple 
        2. Construction method: can have, or can have multiple 
        3. Ordinary member method: can have, or There can be multiple 
        4. Static member methods: there can be, or there can be multiple 
        5. Abstract methods: there can be, or there can be multiple

Use of abstract classes

Although there are construction methods in abstract classes, abstract classes cannot directly create objects!! 
    
The first way to use: //The most common 
    1. Find a subclass and let the subclass inherit the abstract parent class 
    2. Rewrite in the subclass All abstract methods in the parent class. Selectively override the common methods of the parent class 
    3. In the test class, create an object of the subclass, use the subclass object to call the abstract method rewritten by the subclass or the non-private non-private method of the parent class Abstract function 
    
The second way of use: 
    1. Find a subclass and let the subclass inherit the abstract parent class 
    2. Make the subclass itself become an abstract class 
    3. Let the subclass of the subclass implement the parent class and grandpa class All abstract methods 
    4. Create a subclass object of the subclass and call the function

interface interface

Interface interface: one of the three major reference data types in Java; 
Common sense of 
old

interface idea

1. Provide specifications to the outside world -> The specifications are particularly good and bring a lot of convenience 
2. Provide function expansion for the functions of subclasses 
The most important factor in providing specifications 
to the outside world
 

Interface definition format

Format: 
    public interface interface name {//interface name: consistent with the class naming convention big hump 
        
        //defining an interface is also defining a reference data type 
        
    }

Relationship Between Interfaces and Classes

Between classes: inheritance relationship -> single inheritance, multi-layer inheritance 
between classes and abstract classes: inheritance relationship -> single inheritance, multi-layer inheritance 
between 
abstract Between multiple implementation 
interfaces: Inheritance relationship -> multiple inheritance 
​interface
Uncle1Mother{ 
    
} 
​interface
Uncle1Father{ 
​}
 
//Interfaces are inheritance relationships between interfaces, multi-inheritance 
interface Uncle1 extends Uncle1Mother, Uncle1Father{ 
} 
​interface
Uncle2{ 
​}
 
​class
Fu{ 
} 
​//
Multiple implementations of implementation classes 
//A class inherits a parent class to implement multiple parent interface formats 
class Son extends Fu implements Uncle1,Uncle2 { 
}
    
    
    

final

final: final adj. 
    Modification: class class, member variable, member method, local variable 
    
final modified class: no subclass 
final modified member variable: cannot be modified -> constant 
    custom constant: 
        standard format: public static final data type constant Name = initialization value;//Custom constants must be given a value 
        //Naming method of custom constants: all uppercase, multiple words are separated by _ -> JAVA_HOME 
final modification member method: cannot be overridden 
final modification local variables : Cannot be modified -> becomes a constant in the local position//real-time final 
​adj
: The order of public (permission modifier), static, final, and abstract 
    adjectives can be modified 
    
    static public void main(String[] args) { 
        
    }

Members of interfaces prior to JDK_8 (important)

    Interface members before JDK8 version: You can have no 
        custom constants: There are no member variables in the interface, and the defined member variables are all custom constants. The 
            member variables in the interface are modified by public static final by default -> must be custom constant 
        abstraction Method: There can be, or there can be multiple 
            member methods in the interface that are modified by public abstract by default. 
Unavailable
    : 
        1. Member variables: There are no member variables in the interface 
        2. Construction methods: There are no construction methods in the interface -> Interface objects cannot be created directly 
        3. Ordinary member methods: no member methods are allowed in interfaces 
        4. Static member methods: no static member methods are allowed in interfaces

Members of the JDK_8 version of the interface (understanding)

    Interface members of the JDK8 version: there can be no 
        custom constants: there are no member variables in the interface, and the defined member variables are custom constants. The 
            member variables in the interface are modified by public static final by default -> must be custom constant abstract 
        methods : There can be, or there can be multiple 
            member methods in the interface that are modified by public abstract by default 
        ------------------------------- New members --------------------------- 
        default method: can have multiple 
            formats: 
                public default return value type method name (parameter list ){ 
                    //Method body 
                } 
            The default method is modified by public by default 
                1. The default method can be selectively rewritten (the implementation class may not rewrite the default method in the parent interface) 
                2. There is a scenario where the default method must be rewritten- > When the default method of the same declaration appears in multiple parent interfaces, the implementation class must override the default method in the parent interface! How to 
                
            call the default method: Use the implementation class object to call the default method in the interface     
            
       Static method: There can be multiple      
             Format:
                public static return value type method name (formal parameter list) { 
                    //method body 
                }    
            Static methods are modified by public by default. 
            
            Static methods have no concept of rewriting! 
            
            The calling format of static methods: can only be called with interface names!! 
​Not possible
    Yes: 
        1. Member variables: no member variables are allowed in interfaces 
        2. Constructor methods: no constructor methods are allowed in interfaces -> interface objects cannot be created directly 
        3. Ordinary member methods: no member methods are allowed in interfaces

Interface members of JDK_9 version (understand)

    Interface members of the JDK9 version: there can be no 
        custom constants: there are no member variables in the interface, and the defined member variables are custom constants. The 
            member variables in the interface are modified by public static final by default -> must be custom constant abstract 
        methods : There can be, or there can be multiple 
        member 
            methods in the interface that are modified by 
                    public 
            abstract 
                by 
                default 
            . public modification 
                1. The default method can be selectively rewritten (the implementation class may not override the default method in the parent interface) 
                2. The default method must be rewritten in one scenario -> when the same declaration appears in multiple parent interfaces The implementation class must override the default method in the parent interface! 
            The default method call method: Use the implementation class object to call the default method in the interface     
       Static method: There can be multiple      
             formats: 
                public static return value type method name (parameter list){
                
            
                    //Method body  
                }   
            Static methods are modified by public by default. 
            
            Static methods have no concept of rewriting! 
            
            The calling format of static methods: can only be called with interface names!! 
                
        ----------------- --------------Add new members--------------------------- 
        Private methods: 
            
​Not allowed
    Yes: 
        1. Member variables: no member variables are allowed in interfaces 
        2. Constructor methods: no constructor methods are allowed in interfaces -> interface objects cannot be created directly 
        3. Ordinary member methods: no member methods are allowed in interfaces

How to use the interface

There is no constructor in the interface, so it is not possible to create an interface object 
1.
Create a common class (interface implementation class) 
2. Let the implementation class implement the parent interface (implements) 
3. a. Either let the implementation class override the parent interface All abstract methods in 
   b. Either make the implementation class an abstract class, and let the subclass of the implementation class override the abstract method in the interface 
4. In the test class, create an object of the implementation class, and use the object of the implementation class to call the override after method   

Guess you like

Origin blog.csdn.net/m0_70793154/article/details/127166261