Today's content
Interface
Polymorphic
Interface interface
Overview of Interfaces
An interface is a class that is more abstract than an abstract class, because all methods of an interface are abstract methods
The origin of the interface
Can solve the limitations of class single inheritance in Java
Format:
Interface interface Name {
}
Use
Implements, class to implement Interface
Features of the members in the interface
Member variables
Only constants
Default modifier public static final
Member Methods
Only abstract methods, no non-abstract methods
Default modifier public abstract
Construction method
No construction method
The relationship between a class and an interface
Classes and classes: Inheritance relationships (extends), single inheritance, multiple layers of inheritance
Class and Interface: Class implementation (implements) interface (interface), a class can implement multiple interfaces
Interfaces and interfaces: Inheritance relationships (extends) multiple inheritance (one interface can inherit multiple interfaces), multi-layer inheritance
Advantages of the interface
(1) address the limitations of single inheritance
(2) Provision of foreign rules
(3) Reduce the coupling of the program (can achieve modular development, define the rules, everyone to achieve their own modules, improve the efficiency of development)
The difference between an interface and an abstract class
(1) Define the different
Interface is defined by the interface keyword
Abstract class is defined by the abstract class keyword
(2) different from the relationship between classes
Interfaces with classes, classes implement interfaces, can be implemented more
Abstract classes and classes, inheritance relationships, only single inheritance, not multiple inheritance, but can be multi-layered inheritance
(3) Differences in membership
Member variables
Interface: No member variable, only constant, default modifier public static final
Abstract class: Can have member variables, or can have constants
Member Methods
Interface: There is no non-abstract method, there can be only abstract methods, its abstract method is the default decoration, public abstract
Abstract class: There can be either a non-abstract method, or an abstract method, its abstract method is not the default modifier
Construction method
Interface: No construction method
Abstract class: There is a construction method for subclasses to provide
Three main features of object-oriented:
Packaging
Inherited
Polymorphic
Polymorphic
What is polymorphic: polymorphism is a variety of things, such as water can be water, can also become ice; a cat is a cat, an animal.
Su Shi Yue: horizontal as the ridge side into the peak, near and far different
In Java, polymorphism in the parent class is represented by a reference to the object of the subclass, such as animal a = new Cat ();
A: Format of common polymorphic definitions
B: The format of the abstract class polymorphism definition
C: The format of the interface polymorphism definition
Prerequisites for polymorphism:
(1) Having an inheritance or realizing a relationship
(2) There are methods to rewrite
(3) A reference to the parent class object to the child class
Characteristics of members in polymorphic states
Animal a = new Cat ();
Member variables
Compile-time Look Left (parent class), run-time look Left (parent class)
Member method (non-static method) (dynamic binding)
Compile-time See left (parent Class), run-time see right (subclass)
Static methods
Compile look Left (parent class), run-time look Left (parent class)
In short: Compile to the left (parent class), run-time member method (non-static method) look to the right, other (member variables and static methods) look at the left
Type conversions for reference types
Upward transformation: From a small type to a large type
Animal a = new Cat ();
Downward transformation: From a large type to a small type
Cat C = (cat) A;
ClassCastException type conversion exception, converting one type to another, either of the same type or not a parent type
Advantages and disadvantages of polymorphism:
Advantages:
Improved maintainability of the code
Improved Code Extensibility
Disadvantages:
Cannot access subclass-specific members after polymorphism
INSTANCOF operator, INSTANCOF is also a keyword
The function is to determine whether the object (or object name) preceding the operator is a type following the operator
Format:
Object (object name) instanceof type
Note: The use of instanceof is not arbitrary objects and types can be judged together, the type of the object and the type after the operator must have a child parent class relationship,
Otherwise, the compilation will not pass
Java Learning--basic knowledge advanced third day--notes