Today, we introduce three strong features covering, overloading and polymorphism in Java.
Method overrides: When a subclass inherits a parent class, it also inherits the properties and methods of the parent class, can directly use the properties and methods of the parent class, or, if the parent class's methods do not meet the requirements of the subclass, the method of the parent class can be "modified" to overwrite the child class. In the process of overwriting, you need to provide the same method name as the overridden method in the parent class, input parameters, and return types. In addition, when a subclass overrides a method of a parent class, it cannot use more restrictive access than the overridden method in the parent class, such as the method's modifier is public in the parent class, and the overridden method of the subclass cannot be limited by protected,default or private.
Method overloading: In a Java program, it is not possible to have two identical methods in the same class (the same method name, the same return value, and the same argument list) because the compiler cannot relate the invocation of a method to a specific method. However, in a class, if there are multiple methods with the same name, and there are different parameters, this condition is allowed, and this behavior is called overloading of the method. When overloading a method, the parameter list of the method must be different (the number of parameters is different, the parameter data type is different, or both), and the return value of the method can be the same or different. Four basic principles to follow when overloading a method: 1. The method name is the same. 2. The parameter list must be different. 3. The return value can be different. 4. You can call each other.
Polymorphic: In Java, an object variable is polymorphic, and a variable of type AClass can point to an object of type AClass and to any subclass of AClass. The effect of polymorphism is actually to eliminate the coupling between types. If there is a parent class that derives a subclass from it, then we can assign the object of the subclass to the parent class object, which is the embodiment of the "substitution principle" in Java, and whenever the program needs a parent class object, it can be substituted with the object of a subclass, which is also called dynamic binding. However, when using polymorphic pass parameters, it is important to note that an object can have only one exact data type, and if a variable is declared as a reference type of the parent class, the compiler simply treats the object as a parent class type Object even if it refers to an object of a subclass. Therefore, if a reference-type variable is declared as a type of the parent class but actually refers to a subclass object, the variable can no longer access the properties and methods that are added to the child class. This polymorphic mechanism can also be implemented between the interface and the class that implements it, in addition to the polymorphic mechanism between the parent and child classes.
instanceof operator in polymorphic: The polymorphic mechanism in the Java language causes the type of declaration of a reference variable to be inconsistent with its actual reference object, in order to more accurately identify the true type of an object, Java introduces the instanceof operator, using the format: reference type variable The instanceof reference type. This is a Boolean expression that indicates whether the object is a specific instance.
data transformations of reference types: the various simple data types can be converted to each other, some transformations can be done automatically by the system, and some transformations must be done through coercion in the program, and for reference data types there is a mechanism for conversion. There are two cases of custom conversions and casts: converting an instance of a subclass into an instance of a parent class is always possible, and when you convert an object of the parent class to an object of a subclass, you must use the operator "()" to cast the objects to the child class. It is important to note that both automatic conversions and casts can only be used between objects that have an inheritance relationship. Conversion principles for parent and Subclass objects: 1. A subclass object can be considered an object of its parent class. 2. The parent class object cannot be considered an object of one of its subclasses. 3. If the formal parameter of a method defines a parent class object, call this method to use the subclass object as the formal parameter. 4. If the parent object reference is actually a subclass object, the reference to the parent object can be converted into a reference to the subclass object using the coercion type conversion, which is used to judge by the instanceof operator before the conversion.
Overrides, overloads, and Polymorphism in Java