1. The concept of polymorphism
The inheritance relationship allows a subclass to inherit the features of the parent class and attach some new features. A subclass is a specialization of its parent class. Each instance of a subclass is an instance of its parent class, but the other way around is not true. For example: Every circle is a geometric object, but not every geometric object is a circle. Therefore, instances of subclasses can always be passed to parameters that require parent type. Reference cases are as follows:
public class PolymorphismDemo{ /** Main method */ public static void main(String[] args){ displayObject(new Circle(1,"red",false)); displayObject(new Rectangle(1,1,"black",true)); } public static void displayObject(GeometriicObject object){ ("Created on "+ ()+".Color is"+()); } }
Created on Mon Mar 09 19:25:20 EDT is red
Created on Mon Mar 09 19:25:20 EDT is black
Method displayObject has parameters of type GeometricObject. DisplayObject can be called by passing any instance of GeometricObject. Subclass objects can be used wherever parent class objects are used. This is what is usually saidPolymorphic.Simply put,Polymorphism means that variables of parent type can refer to objects of child type。
2. Characteristics of polymorphism
The premise of polymorphism: inheritance (that is, there must be a child-parent relationship.) When calling a method using the parent class reference variable after polymorphism, the method after the subclass is rewritten will be called. Definition format: parent class type Variable name = new Subclass type();
3. instanceof operator
instanceof is the keyword of Java. Every letter in the Java keyword islower caseof.
In order to better understand type conversion, they can be considered similar to the relationship between animals, polar bears, and pandas, where the animal class Animal is the parent class of the polar bear Polar bear and the panda class Panda class. Polar bears are animals, so it is always possible to assign instances of Polar bears safely to Animal variables.The keyword usage is to determine whether an object belongs to a certain data type, the return value is Boolean type.
Fu Zz=new Xu(); Fu Zzz=new yiting(); if(f1 instanceof Xu){ ("Zz is the type of Xu"); } else{ ("Zzz is the type of yiting"); }
4. Polymorphic transformation
1. Upward transformation
The instance of a subclass can always be converted into a variable of a parent class, calledUpconvert, because instances of subclasses are always instances of its parent class.
effect:
Reduce some duplicate code objects to instantiate different objects according to different needs.
package project2; class Animal{ int num=100; void say(){ ("It's an Animal."); } } class Cat extends Animal{ int num=50; void say(){ ("It's a Cat."); } void bark(){ ("Meow Meow!"); } } public class project2{ public static void main(String[] args) { Animal cat=new Cat();//Transform upward (); (); // (); } }
Running results:
100
It's a Cat.
Do not force transformation when transforming upward. The method pointed to or called by the parent class is a subclass method. This is called dynamic binding. After the upward transformation, the parent class reference cannot call the subclass's own methods.
2. Transform downward
Convert an instance of a parent class to its subclass variable. The conversion must be displayed using the conversion tag (subclass name) to indicate your intention to the compiler. For the conversion to be successful, it is necessary to ensure that the object to be converted is an instance of the subclass.
effect:
When you transform upward, other methods unique to subclass objects will be lost; you can transform downward and then redirect.
package project2; class Animal{ int num=100; void say(){ ("It's an Animal."); } } class Cat extends Animal{ int num=50; void say(){ ("It's a Cat."); } void bark(){ ("Meow Meow!"); } } public class project2{ public static void main(String[] args) { Animal cat=new Cat();//Transform upward Cat cat2=(Cat) cat;//Transform downward (); (); (); } }
Running results:
50
It's a Cat.
Meow Meow Meow!
5. Method rewriting
Tip: To override a method, you need to define the method with the same signature as the parent class in the subclass.
Subclasses inherit methods from parent class. Sometimes, a subclass needs to modify the implementation of the method defined in the parent class, which is called method rewriting.
The following points are worth noting:
The rewritten method must have the same signature as the rewritten method, and the same or compatible return type. Compatibility means that the return type of the overridden method can be a subtype of the return type of the overridden method. It can only be rewritten if the instance method is accessible. If the methods defined in the subclass are private in the parent class, then the two methods have no relationship at all. Like instance methods, static methods can be inherited. However, static methods cannot be rewritten. If the static method defined by the parent class is redefined in the subclass, the static method defined in the parent class is hidden. A hidden static method can be called using the syntax "parent class name.static method name". Prevent inheritance and rewrite
Neither class or method modified by final cannot be inherited. The data field modified by final is a constant.
Sometimes, you may want to prevent classes from being inherited. In this case, using the final modifier indicates that a class is the final class and cannot be used as a parent class. The Math class is the final class. The String, StringBuilder and StringBuffer classes, as well as the wrapper classes of all basic data types, are also final classes. For example, the following class A is the final class and cannot be inherited:
public final class A{ //Data fields,constructors, and methods omitted }
It can also define a method as the final method, and the final method cannot be rewritten by its subclass. For example, it cannot be rewritten:
public class Test { //Data fields,constructors, and methods omitted public final void m() { // Do something } }
Note: The modifiers public, protected, private, static, abstract and final can be used on classes and members of classes, and only the final modifier can be used on local variables in the method. The final local variable in the method is a constant.
6. Summary
In order to override a method, the method in the subclass must be defined using the same signature, same or compatible return type as the method in its parent class. The instance method can only be rewritten when it is accessible. In this way, the private method cannot be overwritten because it cannot be accessed outside the class itself. If the methods defined in the subclass are private in the parent class, then these two methods have no relationship at all! Static methods can be inherited just like instance methods. However, static methods cannot be overridden, and if the static methods defined in the parent class are redefined in the child class, the methods defined in the parent class are hidden. You can use the expression obj instanceof AClass to test whether an object is an instance of a class. You can use the final modifier to indicate that a class is the final class and cannot be inherited; it can also indicate that a method is final and cannot be rewritten.
This is the end of this article about the detailed explanation of Java's understanding of polymorphism. For more related Java polymorphism content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!