Java Study Notes 11 (Object-Oriented 4: Polymorphism) and java Study Notes
Polymorphism:
Example: Describes multiple forms of a thing. For example, the Student class inherits the Person class. A Student object is both Student and Person.
Polymorphism is: the parent class reference variable can point to the subclass object
Note: The Sub-parent relationship or the class implementation interface relationship must exist. Otherwise, the polymorphism cannot be completed.
When a method is called using a multi-state parent class to reference a variable, the method rewritten by the subclass is called.
Example:
Public class Fu {public void show () {System. out. println ("show method of the parent class ");}}
Public class Zi extends Fu {public void show () {System. out. println ("subclass override show method of parent class ");}}
Public class Test {public static void main (String [] args) {// exploitation of polymorphism // formula: parent class type (interface type) variable = new subclass object Fu f = new Zi (); f. show () ;}// output: The subclass overrides the show method of the parent class.
Abstract class polymorphism:
public abstract class Person { public abstract void work();}
Public class Teacher extends Person {public void work () {System. out. println ("Teacher at work ");}}
Public class Test {public static void main (String [] args) {// abstract class polymorphism Person p = new Teacher (); p. work () ;}// output: Teacher is working
Interface polymorphism:
public interface Work { public abstract void work();}
Public class Student implements Work {public void work () {System. out. println ("Student at Work ");}}
Public class Test {public static void main (String [] args) {// interface polymorphism Work w = new Student (); w. work () ;}// output: Student at work
Features of member methods in polymorphism:
public class Fu { int a = 1; }
public class Zi extends Fu{ int a = 2;}
Public class Test {public static void main (String [] args) {Fu f = new Zi (); System. out. println (f. a) ;}} // output: 1
Member variables:
During compilation, see whether this variable exists in the parent class. If so, the compilation is successful. If not, the compilation fails.
When running, the variable value in the parent class is run.
Summary: Compile and run all the parent classes
Member method:
A method that is not declared or defined in the parent class cannot be called,
If the subclass is overwritten, The subclass method is called. If the subclass is not overwritten, the parent class method is called.
Conclusion: Compile and view the parent class, and run and view the child class.
Instanceof Keyword:
Used to compare referenced data types:
Public class Test {public static void main (String [] args) {// Person p = new Student (); Person p = new Teacher (); boolean B = p instanceof Student; system. out. println (B) ;}// output: false
Polymorphism Transformation:
Upward Transformation:
Java is a language that forces data types. data involved in calculation must be consistent with each other.
For examplePerson p = new Student ();
The two data types are different but run successfully.
Because there is an automatic type increase here, the value range is automatically increased to a greater value range.
A Child class with a small range, a large range, and a parent class.
Here, the subclass is upgraded to the parent class type.
Advantage: Common Content of sub-classes and parent classes can be called.
Disadvantage: the unique content of the subclass cannot be called.
If you want to call the special content of the subclass:
Downward Transformation (similar to the previously mentioned forced transformation ):
Student s = (Student) p;
The Type forcibly converted from the parent class to the subclass.
Benefits: You can call the unique functions of sub-classes.
Transformation case:
public abstract class Animal { public abstract void eat();}
Public class Cat extends Animal {public void eat () {System. out. println ("cat eating");} public void catchMouse () {System. out. println ("A cat catches a mouse ");}}
Public class Dog extends Animal {public void eat () {System. out. println ("Dog eating");} public void lookHome () {System. out. println ("dog watching ");}}
Public class Test {public static void main (String [] args) {Animal a1 = new Cat (); Animal a2 = new Dog (); a1.eat (); a2.eat (); // perform a downward transformation and call the special method of the subclass. // use if. instanceof. to ensure security, if (a1 instanceof Cat) {Cat c = (Cat) a1; c. catchMouse ();} if (a2 instanceof Dog) {Dog d = (Dog) a2; d. lookHome () ;}}/ * output: * The cat is eating * the dog is eating * the cat is catching the mouse * the dog is watching */