Polymorphism is a concept that has never been clearly understood. Through this note, I have a comprehensive understanding of the concept and application of polymorphism, and learned the characteristics of the instanceof keyword and object class.
1. polymorphism of classes in Java
The so-called class polymorphism refers to the mutual conversion between the Child class object and the parent class object. Here there are two parts to take notes:
1.1 convert subclass objects to parent objects (up conversion))
The conversion from a subclass object to a parent object isAutomatic conversion. Format: parent class Object = subclass object.
Note: After a subclass object is converted to a parent class object, the called method must be the method after the child class is rewritten, rather than the original method of the parent class.
For example:
Class person {public void fun1 () {system. out. println ("person -------> Public void fun1 ()");} public void fun2 () {// fun2 () calls the fun1 () method this. fun1 () ;}} class student extends person {// The fun1 () method public void fun1 () {system. out. println ("Student -------> Public void fun1 ()");} public void fun3 () {system. out. println ("Student -------> Public void fun1 ()") ;}} public class demo01 {public static void main (string ARGs []) {// declare a parent class Object // new student () is to instantiate a subclass object, convert to the parent class Object // After the subclass object is converted to the parent class object, the called method must be the method person P = new student (); p. fun1 (); p. fun2 (); // This is also through this. fun1 ()}}
1.2 convert a parent class object to a subclass object (downward conversion)
The conversion from a parent class object to a subclass object isForced conversion. Format: subclass object = (subclass name) parent class Object
Note the following when converting a parent class object to a subclass object: If the parent class object is an instantiated parent class object instead of an object converted from a subclass object, it cannot be forcibly converted to a subclass object; otherwise, a classcastexception class conversion exception occurs. That is to say,The premise that a parent class object can be converted to a subclass object is that the parent class object was previously initialized using the instance of the subclass object.. In the following example, the instantiated subclass object is first converted to the parent class object.
Class person {public void fun1 () {system. out. println ("person -------> Public void fun1 ()");} public void fun2 () {// fun2 () calls the fun1 () method this. fun1 () ;}} class student extends person {// The fun1 () method public void fun1 () {system. out. println ("Student -------> Public void fun1 ()");} public void fun3 () {system. out. println ("Student -------> Public void fun1 ()") ;}} public class demo02 {public static void main (string ARGs []) {// declare a parent class object, new student () is to instantiate a subclass object, which is converted to a parent class Object // the conversion from a parent class object to a subclass object must be forcibly converted. /* Note: if a parent class object is instantiated as a parent class object instead of a subclass object in the following example and is converted to a parent class object, then it cannot be forcibly converted to a subclass object, otherwise, a classcastexception class conversion exception */person P = new student (); student s = (student) P; S. fun3 (); S. fun1 ();}}
Conclusion:
Before performing a downward conversion, the two objects must first undergo an upward Conversion Relationship (for example, in the preceding example); otherwise, they cannot be converted (a class conversion exception occurs ).
2. instanceof keyword
Instanceof is used to determine whether an object is an instance of a class. For example, if a instanceof B is an instance of B, true is returned; otherwise, false is returned.
Time to use instanceof:
In the following example, generally, instanceof is used before the transformation, so that you can perform a safer type conversion .
Class A {public void fun1 () {system. out. println ("A --> Public void fun1 ()");} public void fun2 () {// The fun2 method calls the fun1 method this. fun1 () ;}} Class B extends a {// override the fun1 () method public void fun1 () {system. out. println ("B --> Public void fun1 ()");} public void fun3 () {system. out. println ("B --> Public void fun3 ()") ;}} Class C extends a {// override the fun1 () method public void fun1 () {system. out. println ("c --> Public void fun1 ()") ;} Public void fun5 () {system. out. println ("c --> Public void fun5 ()") ;}} public class demo03 {public static void main (string ARGs []) {fun (New C ());} /* If the input is a Class B object, you need to call the fun3 () method again. If the input is a class C object, you need to call the fun5 () method again. In this way, through instanceof, the instance */public static void fun (A) {. fun2 (); // if A is the instantiated object of B, call fun3 () if (a instanceof B) {B = (B) A; B. fun3 ();} // if A is an instantiated object of B, call fun5 () if (a instanceof c) {c = (c) A; C. fun5 ();}}}
3. Object Class
All classes are inherited from the object class, which is a base class of all classes.
For a better class, three methods in the object class need to be rewritten.
|-Public String tostring ()
|-Public Boolean equals (Object OBJ)
|-Public int hashcode ()
In the following example, student describes the tostring () method and equals () method, and uses the instanceof keyword, which is a small review question ~
Class student // any class inherits the object class. It is best to rewrite the tostring () and equals () methods {private string name; private int age; Public student (string name, int age) {This. name = Name; this. age = age;} // rewrite the tostring () method in the object class to implement custom tostring () Public String tostring () {return "the student's name is: "+ name +", the student's age is: "+ age;} // rewrite the equals () method in the object class. Note that equals () in the object class () the received object is public Boolean equals (Object OBJ) {// if the memory address of the object to be compared is the same as that of the object to be compared, it must be the same object // note: The memory address is compared with "=" If (this = OBJ) {return true;} else {// If obj is not a student instance, it is definitely not equal, returns false; If (! (OBJ instanceof student) {return false;} else {// This step shows that obj is the Instance Object of student, and student Stu = (student) OBJ after type conversion; if (this. name. equals (Stu. name) & this. age = Stu. age) {return true;} else {return false ;}}} public class demo04 {public static void main (string ARGs []) {student stu1 = new student ("newsainton", 23); Student stu2 = new student ("newsainton", 23); // use the tostring () method to output the stu1 object system. out. println (stu1.tostring (); // use the tostring () method to output the stu2 object system. out. println (stu2.tostring (); // compare whether stu1 and stu2 are equal to system. out. println ("compare stu1 and stu2 for Equality:" + stu1.equals (stu2 ));}}