The Java object-oriented polymorphic continuation
Polymorphic: A reference to a parent type can point to an object of a subtype.
Late binding is the determination of the type when executing the run, not the time of compilation.
Parent p = new Child (); When calling a method using polymorphic mode, first check if the parent class has the method such as sing (), if there is no compile error, if any, then call the subclass of the method, such as sing ().
There are two types of coercion type conversions:
1). Up-type conversion (UPCAST): For example, convert the cat type to the animal type and convert the subtype to the parent type. For up-type conversions, you do not need to specify explicitly.
2). Down type conversion (downcast): For example, convert the animal type to cat type. Converts the parent type to a subtype. For down conversions, you must explicitly specify (you must use coercion type conversions).
Polymorphic Classic Use:
public class car{
public void Run () {}
}
public class BMW extends car{
public void Run () {}
}
public class VW extends car{
public void Run () {}
}
public class testcar{
public static void Main (string[] args) {
Testcar TC = new Testcar ();
Car BMW = new BMW
Tc.test (BMW);
Car VW = new VW ();
Tc.test (VW);
}
public void test (car car) {
Car.run ();
}
}
The test () method receives a car parent class parameter and calls its method. But when actually used, the object that is passed is the subclass, which corresponds to the method that calls the subclass. This allows subclasses to use this method.
Java records -15-of object-oriented polymorphism continuation