Interface
Summary:
- It is a reference type in the java language and a collection of methods. The interface encapsulates methods, including abstract methods (JDK7), default methods and static methods (JDK8), and private methods (JDK9). The interface will also be compiled into a. class file, but it is not a class, but a reference data type.
Format:
-
public interface Interface name{ //Abstract methods: for subclass implementations public abstract void method1(); //Default method, default cannot be omitted, for subclass calls or overrides public default void mothod2(); //Static methods for direct interface calls public static void mothod3(); //Private methods, private static methods, for use by default methods or static methods in interfaces private void mothod4() { //... } }
The relationship between the implementation class of the interface and the interface is the realization relationship, that is, the class implementation interface, which is called the implementation class of the interface, or the subclass of the interface. The implementation uses the implementation keyword.
Non-abstract subclass implementation
- All abstract methods in the interface must be overridden
- It inherits the default method in the interface, which can be called directly or rewritten.
format
-
Class class name implement interface name{ // Rewrite Abstract methods. // When overriding the default method, the default keyword is not retained }
Multiple Implementation of Interface
- A class can implement multiple interfaces, called multiple implementations of interfaces, and a class can inherit a parent class and implement multiple interfaces at the same time.
-
Class subclass name [extends parent class name] implement interface 1, interface 2, interface 3...{ // Rewrite abstract methods in interfaces... }
- When the default method in the parent class and interface is renamed, the child class executes the method in the parent class close to it.
Multiple Inheritance of Interfaces
- Interfaces can inherit multiple interfaces, and the inheritance of interfaces is modified with extends keywords. If the parent interface has a default method rename, the child interface must be rewritten once, and the default keyword should be retained.
Other characteristics
- Membership variables cannot be defined in the interface, but constants can be defined, and public static final modifiers are used by default (the latter constants are best capitalized and underlined to separate words)
- There is no constructor in the interface, so objects cannot be created
- There is no static code block in the interface
polymorphic
Definition:
- It refers to the same kind of behavior, which has many different forms of expression.
Format representation of polymorphism
- A parent reference points to a child class object
-
Variable name of parent type = new subclass type (); Fu z = new Zi();
- When calling a method in a polymorphic way, first check whether there is a method in the parent class, if there is no, compile errors, and if there is, execute a subclass override method.
Benefits of polymorphism
- In the actual development process, the parent type as a formal parameter of the method, passing the subclass object to the method, and calling the method, can better reflect the expansion and convenience of polymorphism.
Reference Data Type Conversion
-
Upward Transition: Polymorphism itself is the process of converting subclass types to parent types, which is the default process.
- Downward transition: the process of conversion from parent type to subclass type.
-
Subclass type variable name= (subclass type) parent variable name; Cat c = (cat) a;
- Significance of downward transition: When using polymorphic invocation methods, methods owned by subclasses but not in parent classes can not be invoked, so if you want to invoke methods specific to subclasses, you need to downward transition.
-
// Definition class public abstract class Animal{ public abstract void eat(); } public class Cat extends Animal { @Override public void eat() { System.out.println("Eat fish"); } public void catchMouse() { System.out.println("Catching mice"); } } public class Dog extends Animal { public void eat() { System.out.println("Eat Bones"); } public void watchHouse() { System.out.println("Housekeeping"); } } //Define test classes public class Test { public static void main(String[] args) { // Upward transformation Animal a = new Cat(); a.eat(); // The call is Cat's eat // Downward transition Cat c = (Cat)a; c.catchMouse(); // The catchMouse of Cat is called. } }
Type conversion exception
public class Test { public static void main(String[] args) { // Upward transformation Animal a = new Cat(); a.eat(); // The call is Cat's eat // Downward transition Dog d = (Dog)a; d.watchHouse(); // The call is Dog's watchHouse [Run Error Reporting] } }
- Because Cat objects are created, they cannot be converted to Dog objects. To avoid the occurrence of ClassCastException, you can use the instance of keyword
-
/* Variable name instanceof data type If the variable belongs to this type, return true or false */ public class Test { public static void main(String[] args) { // Upward transformation Animal a = new Cat(); a.eat(); // The call is Cat's eat // Downward transition if (a instanceof Cat){ Cat c = (Cat)a; c.catchMouse(); // The catchMouse of Cat is called. } else if (a instanceof Dog){ Dog d = (Dog)a; d.watchHouse(); // The call is Dog's watchHouse } } }