Concept of polymorphism
- Polymorphism mainly refers to the multiple forms of the same thing.
- Beverages: coke, Sprite, red bull, pulse
- Pets: cats, dogs, birds, cockroaches, fish
- People: students, teachers, workers, security guards
- Graphics: rectangle, circle, trapezoid, triangle
Polymorphic syntax format
- Parent type reference variable name = new subclass type ();
- For example:
Shape sr = new Rect();
sr.show();
Case title
- The encapsulation of Shape class is realized by programming. The features include: abscissa and ordinate. It is required to provide a method to print all features.
- Program to realize the encapsulation of Rect class and inherit from Shape class. The characteristics are: length and width.
- The ShapeRectTest class is programmed to create Shape and Rect type objects and print features in the main method.
Polymorphic characteristics
- When the reference of the parent type points to the object of the child type, the reference of the parent type can directly call the methods unique to the parent class.
- When the reference of the parent type points to the object of the child type, the reference of the parent type cannot directly call the methods unique to the child type.
- For non static methods of both parent and child classes, the compilation stage calls the parent version, and the runtime stage calls the overridden version of the child class (dynamic binding).
- For static methods of both parent and child classes, the version of the parent class is called both at compile and run time.
Conversion between reference data types
- There are two conversion methods between reference data types: automatic type conversion and forced type conversion.
- Automatic type conversion mainly refers to the conversion from small type to large type, that is, the subclass is converted to parent, also known as upward transformation.
- Forced type conversion mainly refers to the conversion from a large type to a small type, that is, the parent class is converted to a child class, which is also called downward transformation or explicit type conversion.
- The conversion between reference data types must occur between parent and child classes, otherwise the compilation will report an error.
- If the target type of the forced conversion is not the data type that the reference really points to, the compilation passes, and a type conversion exception occurs at the runtime.
- In order to avoid the occurrence of the above errors, judgment should be made before forced rotation. The format is as follows:
- If (reference variable instanceof data type)
- Judge whether the object pointed to by the reference variable is the following data type
Practical significance of polymorphism
- The practical significance of polymorphism is to shield the differences of different subclasses and realize general programming to bring different effects
package demo;
public class Shape {
private int x;
private int y;
public Shape() {
}
public Shape(int x, int y) {
setX(x);
setY(y);
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void show() {
System.out.println("Abscissa:" + getX() + ",Ordinate:" + getY());
}
// Custom static method
public static void test() {
System.out.println("Shape Class!");
}
}
package demo;
public class Rect extends Shape {
private int len;
private int wid;
public Rect() {
}
public Rect(int x, int y, int len, int wid) {
super(x, y);
setLen(len);
setWid(wid);
}
public int getLen() {
return len;
}
public void setLen(int len) {
if(len > 0) {
this.len = len;
} else {
System.out.println("Unreasonable length!!!");
}
}
public int getWid() {
return wid;
}
public void setWid(int wid) {
if (wid > 0) {
this.wid = wid;
} else {
System.out.println("The width is unreasonable!!!");
}
}
@Override
public void show() {
super.show();
System.out.println("The length is:" + getLen() + ",The width is:" + getWid());
}
// Custom static method
//@Override Error: historical reasons, not rewriting in the real sense
public static void test() {
System.out.println("---Rect Class!");
}
}
package demo;
public class ShapeRectTest {
public static void main(String[] args) {
// 1. Declare that the reference of Shape type points to the object of Shape type and print the feature
Shape s1 = new Shape(1, 2);
// When the show method is not overridden in the Rect class, the show method in the Shape class is called below
// When the show method is overridden in the Rect class, the show method in the Shape class is called below
s1.show(); // 1 2
// Use the ctrl+d shortcut to copy the current row
System.out.println("------------------------------------");
// 2. Declare that the reference of Rect type points to the object of Rect type and print the characteristics
Rect r1 = new Rect(3, 4, 5, 6);
// When the show method is not overridden in the Rect class, the show method in the Shape class is called below
// When the show method is overridden in the Rect class, the show method in the Rect class is called below
r1.show(); // 3 4 5 6
// Use alt+shift + up and down arrow keys to move the code
System.out.println("------------------------------------");
// 3. Declare that the reference of Shape type points to the object of Rect type and print the feature
// It is equivalent to the conversion from Rect type to Shape type, that is, the conversion from child class to parent class, and the conversion from small to large is automatic type conversion
Shape sr = new Rect(7, 8, 9, 10);
// When the show method is not overridden in the Rect class, the show method in the Shape class is called below
// After rewriting the show method in Rect class, the following code calls the method of Shape class in the compilation stage and the show method in Rect class in the run stage
sr.show(); // 7 8 9 10
System.out.println("------------------------------------");
// 4. Test whether the Shape type reference can directly call the unique methods of the parent and child classes???
int ia = sr.getX();
System.out.println("The abscissa obtained is:" + ia); // 7
//sr.getLen(); Error the getlen method cannot be found in the Shape class, that is, it is also found in the Shape class
// Call static method
sr.test(); // Tip: access by referencing. Is not recommended
Shape.test(); // It is recommended to use the class name
System.out.println("------------------------------------");
// 5. How to use the reference of the parent class type to call the method unique to the child class
// It is equivalent to the conversion from Shape type to Rect type, that is, the conversion from parent class to child class, large to small conversion, and forced type conversion
int ib = ((Rect) sr).getLen();
System.out.println("The obtained length is:" + ib); // 9
// If you want to convert Shape type to String type, mandatory type conversion requires a parent-child relationship
//String str1 = (String)sr; Error
// You want to cast Shape type to Circle type, and there are no errors below
//Circle c1 = (Circle)sr; // Compilation ok, but ClassCastException type conversion exception occurred during the runtime
// You should use instanceof to determine the type before casting
// Judge whether the object in the heap memory pointed by sr is of type Circle. If so, return true; otherwise, return false
if(sr instanceof Circle) {
System.out.println("You can safely convert!");
Circle c1 = (Circle)sr;
} else {
System.out.println("There are risks in forced conversion, and the operation should be cautious!");
}
}
}