1. Polymorphism
1. What is polymorphism?
(inheritance is the premise of polymorphism)
Polymorphism refers to various forms in popular terms. Specifically, it refers to completing a certain behavior. When different objects complete it, different forms will be produced.
At the code level, each object of the subclass is also the object of the parent class. For example, every manager is an employee, but on the contrary, not every employee is a manager. This is the "is-a" rule in inheritance. Another expression of it is the replacement rule, that is i to say, the subclass object can be used to replace any place where the parent object appears in the program.
2. How to use polymorphism?
The polymorphism embodied in the code is actually that the parent class reference points to the subclass object
There are two general formats:
1. Parent class name = object name = new = subclass name ();
2. Interface name object name = new implementation class name ();
Access characteristics of member methods and member variables in polymorphism
public class People { String name; int age; public People(String name, int age) { this.name = name; this.age = age; } public void eat(){ System.out.println(age+"Year old"+name+"I am eating!"); } } public class Student extends People{ //The student class inherits the people class public Student(String name, int age) { super(name, age); } @Override public void eat() { System.out.println(age+"Year old"+name+"I'm having a student meal!"); } } public class Main { public static void eat(People a){ //Use a variable a of People class to receive the variable. We don't know what type of variable a is a.eat(); } public static void main(String[] args) { People people = new People("Zhang San",38); Student student = new Student("Li Si",15); People student2 = new Student("Wang Wu",18); eat(people); eat(student); System.out.println(student2.age); System.out.println(student2.name); } }
The operation result is:
In the above code, we found that to realize polymorphism in Java, we must meet the following conditions, none of which is indispensable:
1. Must be under the inheritance system;
2. Subclasses must override the methods in the parent class;
3. Call the overridden method through the reference of the parent class;
In polymorphism, the access characteristics of member methods are:
Call whoever is new first
In polymorphism, the access characteristics of member variables are:
1. Access member variables directly through the object name (see who is on the left of the equal sign, give priority to who is used, and look up if not);
2. Access member variables indirectly through member methods (see who the method belongs to, give priority to who uses it, and look up if there is no one);
In fact, polymorphic writing is an upward transformation operation.
Upward transformation
The meaning of upward transformation is to create a subclass object and treat it as a parent class. The writing method is polymorphic.
People student2 = new Student("Wang Wu",18);
In this code, student2 is a parent type, but can reference a subclass object, because the subclass object is a parent object, that is, a subclass object can be used as a parent object. Therefore, upward transformation is safe, because it is a transformation from small scale to large scale.
However, upward transformation also has disadvantages:
public class Student extends People{ //The student class inherits the people class public Student(String name, int age) { super(name, age); } @Override public void eat() { System.out.println(age+"Year old"+name+"I'm having a student meal!"); } public void sleep(){ System.out.println(name+"Students sleep in class"); } }
public class Main { public static void eat(People a){ //Use a variable a of People class to receive the variable. We don't know what type of variable a is a.eat(); } public static void main(String[] args) { People people = new People("Zhang San",38); Student student = new Student("Li Si",15); People student2 = new Student("Wang Wu",18); student.sleep(); student2.sleep(); } }
We found that the code reported an error.
Once the upward transformation is transformed into the parent class, the original unique content of the child class cannot be called.
How to solve this situation? Restore with the downward transformation of the object.
Downward transformation
The downward transformation of an object is actually a restore action.
Format:
Subclass name object name = (subclass name) parent object;
Meaning: restore the parent object to the child object.
The subclass name in parentheses should be the class that the parent class object behind it originally points to. If not, the runtime will throw an exception.
Downward transformation uses less and is unsafe. In case of conversion failure, the runtime will throw an exception. In order to improve the security of downward transformation in java, instanceof is introduced. If the expression is true, it can be safely transformed.
Format:
Object name instanceof class name
The return value type of this expression is Boolean, which is to judge whether the previous object can be used as an instance of the following type.
For example:
public class Main { public static void eat(People a){ //Use a variable a of People class to receive the variable. We don't know what type of variable a is a.eat(); } public static void main(String[] args) { People people = new People("Zhang San",38); Student student = new Student("Li Si",15); People student2 = new Student("Wang Wu",18); student.sleep(); if(student2 instanceof Student){ //Use instanof to verify the validity of the downward transformation operation Student student1 = (Student) student2; student1.sleep(); } }
Operation results:
We see that this can solve the problem that the upward transformation cannot call the methods specific to subclasses. In the validity verification of downward transformation, we restore student2 to student1, and call the special method sleep () of Student class through it.
final keyword
Final means final in English
final as a keyword can be used to
1. Modify a class;
2. Modify a method;
3. Modify a local variable;
4. Modify a member variable;
1. When final modifies a class
Format:
public final class name{
...............
}
When final is used to modify a class, it means that this class cannot have any subclasses (we can call it eunuch class)
The final modified class cannot be used as a parent class, because it is a eunuch class and cannot be inherited.
2. When final modifies a method, the method cannot be overridden.
Note: in java, we require that the methods modified by abstract must be overwritten and rewritten, while the final keyword is the opposite, so both abstract and final keywords cannot be used at the same time.
3. Once final is used to modify a local variable, the variable cannot be modified, that is, "once assigned, it will remain unchanged for life", and can only be assigned once. For basic data types, immutability means that the data in the variable is immutable; For reference data types, immutability means that the address value in the variable is immutable, but the variable in the class can still be modified.
4. For member variables, if they are modified with the final keyword, their variables are still immutable. Since the member variable has a default value, it must be assigned manually after final is used, and the default value will not be given again
At this point, we have two methods to assign values to it
1. Use direct assignment
public class Student extends People{ final int classroom = 1; //Direct assignment //The student class inherits the people class public Student(String name, int age) { super(name, age); } @Override public void eat() { System.out.println(age+"Year old"+name+"I'm having a student meal!"); } public void sleep(){ System.out.println(name+"Students sleep in class"); } }
2. Assignment by construction method
public class Student extends People{ final int classroom ; //Direct assignment //The student class inherits the people class public Student(String name, int age) { super(name, age); classroom = 1; //Assign a value to it in the construction method } @Override public void eat() { System.out.println(age+"Year old"+name+"I'm having a student meal!"); } public void sleep(){ System.out.println(name+"Students sleep in class"); } }
It should be noted that:
It must be ensured that all overloaded construction methods in the class will eventually assign values to the variables modified by final!