polymorphic
definition
-
The same method can adopt a variety of different behavior modes according to different sending objects.
-
The actual type of an object is determined, but there are many types of references that can point to the object (uncertainty)
The methods that can be called by a subclass are their own or inherit from the parent class; The methods that the parent class can call can only be its own or those in the Object class. Although the parent class can point to the child class, it cannot call the methods unique to the child class! If the subclass overrides the method of the parent class, the subclass calls with its own method, not the method of the parent class!
Precautions for polymorphism:
- Polymorphism is the polymorphism of methods, and there is no polymorphism of attributes
- There is a relationship between the parent class and the child class, otherwise a type conversion exception (ClassCastException) will occur
- Existence conditions: inheritance relationship, method needs to be overridden, and parent class reference points to child class object! Father f1 = new Son();
- Methods that cannot be overridden:
- static method belongs to class, not instance
- final: indicates a constant
- Private method: a private method and cannot be overridden
import java.util.Scanner; public class practice { public static void main(String[] args) { Scanner in=new Scanner(System.in); Student s1=new Student(); Person s2=new Student(); //A reference to a parent class points to a child class Object s3=new Student(); //This shows that the actual type of an object is determined, but the reference type is uncertain! s1.run();//The subclass overrides the method of the parent class and executes the method of the subclass s2.run(); s1.eat(); /*s2.eat(); An error will be reported here because the method executed by the object is mainly related to the type on the left, not the type on the right*/ in.close(); } } class Person{ public void run(){ System.out.println("Person"); } } class Student extends Person{ @Override public void run() { System.out.println("student"); } public void eat(){ System.out.println("eat something!"); } } /*Operation results: student student */
instanceof keyword
instanceof: it can be used to judge whether there is an inheritance relationship between two classes
x instanceof y: true if x is the parent of y, otherwise false
import java.util.Scanner; public class practice { public static void main(String[] args) { Scanner in=new Scanner(System.in); Object obj=new Student(); //Object > Person >Student //Object > Person >Teacher //Object > String System.out.println(obj instanceof Object);//true System.out.println(obj instanceof Person);//true System.out.println(obj instanceof Student);//true System.out.println(obj instanceof Teacher);//false System.out.println(obj instanceof String);//false System.out.println("========================================="); Person person =new Student(); System.out.println(person instanceof Object);//true System.out.println(person instanceof Person);//true System.out.println(person instanceof Student);//true System.out.println(person instanceof Teacher);//false /* System.out.println(person instanceof String);An error will be reported here. The Person class here has no inheritance relationship with the String type*/ System.out.println("========================================="); Student student=new Student(); System.out.println(student instanceof Object);//true System.out.println(student instanceof Person);//true System.out.println(student instanceof Student);//true /*System.out.println(student instanceof Teacher);An error will be reported here because Teacher and Student are subclasses of Person and have no inheritance relationship*/ // System.out.println(person instanceof String); in.close(); } } class Person{ public void run(){ System.out.println("Person"); } } class Student extends Person{ } class Teacher extends Person{ } /*Operation results: true true true false false ========================================= true true true false ========================================= true true true */
Conversion relationship between subclass and parent class:
Convert a child class to a parent class: cast; A parent class cannot be converted to a child class, but it can receive a child class, which is similar to redefining a variable to accept the data of a child class!
import java.util.Scanner; public class practice { public static void main(String[] args) { Scanner in=new Scanner(System.in); //High and low Person student=new Student(); //student.go(); An error will be reported here because there is no go method in the Person class Student obj=(Student) student; System.out.println("==============="); ((Student) student).go(); System.out.println("================"); obj.go(); //Some methods may be lost when converting a subclass to a parent class! Student stu=new Student(); System.out.println("==============="); stu.go(); Person person=stu; /*person.go();An error will be reported here. If a parent class is defined to receive a child class, the newly defined parent class can receive all the parent class methods of the child class, but the methods of the child class will be lost*/ in.close(); } } class Person{ public void run(){ System.out.println("Person"); } } class Student extends Person{ public void go(){ System.out.println("go"); } @Override public void run() { System.out.println("Student"); } } class Teacher extends Person{ } /*Operation results: =============== go ================ go =============== go*/
static
import java.util.Scanner; public class practice { public static int Id;//Static variable public String name;//Non static variable public static void main(String[] args) { Scanner in=new Scanner(System.in); Id=1212; System.out.println(Id); System.out.println("==========================="); practice.Id=520; System.out.println(Id); System.out.println("==========================="); //name="good!"; An error will be reported here because name is not a static attribute. You need to new an object first! //practice.name="good!"; There will also be errors here. Similarly practice ps=new practice(); ps.name="good!"; System.out.println(ps.name); in.close(); } } /*Output results: 1212 =========================== 520 =========================== good! */
Static variables have only one memory in all their classes, that is, static variables can be modified in all classes
Non static methods can call all things in static methods, while static methods can only call static methods, not non static methods (related to the loading order of classes!)
Code block:
import java.util.Scanner; public class practice { // Execution order: static code block anonymous code block constructor // The static code block is executed only once, that is, it is executed at the first new, and then it will not be executed at new { //Anonymous code block! Generally used to assign initial values System.out.println("Anonymous code block!"); } static { //Static code block! System.out.println("Static code block!"); } public practice() { System.out.println("Constructor!"); } public static void main(String[] args) { Scanner in=new Scanner(System.in); practice ps=new practice(); System.out.println("=================="); practice pb=new practice(); in.close(); } } /*Output results: Static code block! Anonymous code block! Constructor! ================== Anonymous code block! Constructor! */
abstract class
All methods of an abstract class and subclasses that inherit it must implement its methods!
characteristic:
- You can't new, you can only test subclasses to implement it;
- Ordinary methods can be written in abstract classes;
- Abstract methods must be in abstract classes;
- Abstract methods only have method names, no method implementations!