Java object-oriented (5): polymorphism

Polymorphism

The concept of polymorphism:
Polymorphism is the third characteristic of object-oriented after encapsulation and inheritance.

Real things often reflect many forms, such as students, students are a kind of human, then a specific classmate is both a student and a human, that is, there are two forms. As an object-oriented language, Java can also describe multiple forms of a thing. If the Student class inherits the Person class, the object of a Student is both Student and Person.
The polymorphic code in Java is embodied in a subclass object (implementation class object) that can not only assign values ​​to the subclass (implementation class object) reference variable, but also to the parent class (interface) variable of this subclass (implementation class object) Assignment. For example, the Student class can be a subclass of the Person class. Then a Student object can be assigned to a reference of type Student or a reference of type Person.
Ultimately, polymorphism is reflected in the parent class reference variable can point to an instance of the child class object. The premise of polymorphism is that there must be a child-parent relationship or a class implementation interface relationship, otherwise polymorphism cannot be completed. When using the polymorphic parent class reference variable to call the method, the subclass rewritten method will be called. The same method produces different behaviors and results because of different objects.

Conditions for the production of polymorphism:

  • 1. There must be a child parent or class to implement the interface relationship
  • 2. Subclass overrides the method of the parent class
  • 3. The parent class reference points to the child class

The definition format of polymorphism
The definition format of polymorphism : that is, the reference variable of the parent class points to the subclass object; the
parent type variable name = new subclass type ();
variable name. method name ();
ordinary class polymorphic definition Format :
parent class variable name = new subclass();

	举例:

Insert picture description here
Abstract class polymorphism definition format :

抽象类 变量名 = new 抽象类子类();
 举例:

Insert picture description here
The format of interface polymorphism definition :

接口 变量名 = new 接口实现类();
举例:

Insert picture description here
Polymorphism application example 1
Insert picture description here
Polymorphism application example 2
 Using the parent class as the formal parameter of the method
Insert picture description here
Polymorphism application example 3
 Using the parent class as the return value of the method
Insert picture description here
Note
• The method of the same parent class will be different children Class rewriting. When calling the method, the method that is called is the rewritten method for each subclass.
Such as:

		 Person p1 = new Student();
	     Person p2 = new Teacher();
	     p1.work(); //p1会调用Student类中重写的work方法
	     p2.work(); //p2会调用Teacher类中重写的work方法

The actual meaning of polymorphism:

 The equals (Object o) method in the Object class, if there is no polymorphism, then multiple equals methods must be written.

 doData(Connection cnn), where Connection is an interface, which can pass the specific implementation of different database vendors, otherwise, to write specific connection objects, the versatility is too poor to achieve decoupling.

 Polymorphism does not apply to attributes, because attributes cannot be overridden. Pay special attention to this! ! !
Insert picture description here

Type conversion of reference types

Downcasting (downcasting)
• A subclass object that has been upcast can use the mandatory type conversion format to convert the parent class reference to the subclass reference. This process is downcasting. If the parent object is created directly, it cannot be downcast.
• Use format:
subclass type variable name = (subclass type) variable of the parent type;
Insert picture description here
upcasting (upcasting)
• When a subclass object is assigned to a parent class reference, it is upcasting , and polymorphism itself is upcasting The process of transformation.
• Use format:
parent type variable name = new subtype type();
such as: Pet p = new Dog();//Pet object cannot call subclass specific methods

Rules for converting subclasses to parent classes:

  • 1. Point a reference of a parent class to a subclass object, which is called up-casting, and type conversion is performed automatically;
  • 2. At this time, the method called by the parent class reference variable is the method of the subclass covering and inheriting the parent class, not the method of the parent class;
  • 3. At this time, the method specific to the subclass cannot be called through the parent class reference variable; the subclass can only be called to override the method inherited from the parent class.

Instanceof

Instanceof comparison operator (comparison reference data types)
role
can determine if an object belongs to a certain data type instanceof keyword. If
the object of the student belongs to the student category, the object of the student also belongs to human beings;

Format : boolean b = object instanceof data type;

For example:
Insert picture description here

Object class

java.lang.Object;
类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法。 

1. The base class of
all classes; 2. All classes have methods defined in the Object class;
3. Rewrite the toString() method according to the API documentation;

Common methods:
• public int hashCode(): returns the hash code value of the object;
• public boolean equals(Object obj): indicates whether some other object is "equal" to this object.
• public String toString(): Returns the string representation of the object. Usually, the toString method returns a string that "represents" this object as text. The result should be a concise but easy-to-read information expression. It is recommended that all subclasses override this method.

The equals() method
of the Object class • The equals method of the Object class compares the addresses of two objects; you can see it by looking at the source code of the Object class.
• String, Date, File, and packaging classes, they all rewrite the equals method of the Object class, so they compare the "specific content".

Rewrite the equals method to demonstrate:
define the Person class, id and name two attributes, if these two attributes are the same, it is considered the same person; that is, the equals method returns true.

Note: You can directly use the eclipse tool to generate directly, directly click the right mouse button—>Source—>Generate hashCode() and equals()...
Insert picture description here
Insert picture description here
The toString() method of the Object class

 The toString method in the Object class returns the string form of an object. Looking at the source code of Object's toString method, you can understand why a weird string of characters is output.

 When we output a reference to an object, the toString() method of this object will be called automatically.
Insert picture description here

 Demonstration of overriding the toString() method of the Person class

 Demonstrated by Eclipse, automatically rewriting the toString method is
Insert picture description here
often used as an interview written test question:

What is the difference between final, finally and finalize?

Why do I need to rewrite the hashCode method when rewriting the equals method?

Continue studying…

Guess you like

Origin blog.csdn.net/zhangzhanbin/article/details/111655209