Polymorphism: the different states of something at different times. For example, water is liquid, solid and gaseous.
Prerequisites for polymorphism:
1. There should be inheritance
2. There should be rewriting of methods. It doesn't make much sense without rewriting.
3. The reference of the parent class should point to the object of the child class: parent class name f =new child class name (.);
Characteristics of polymorphic access members:
1. Member variables are compiled to the left and run to the left
2. When the constructor creates a subclass object, first access the constructor in the parent class and initialize the data of the parent class. 3. Look at the left when compiling the member method and the right when running. Because there is an override of the member method, look to the right for access
4. Static member methods are compiled and run on the left. Because the members modified by static are related to classes, they are not rewritten here, so when running, they are accessed on the left.
Disadvantages of polymorphism 1: polymorphism cannot access methods with the same method name in the parent class
Solution: do not use polymorphism
Drawback 2: polymorphism cannot access methods unique to subclasses
Solution: transition down, similar to cast
Format: subclass class name variable name = (subclass class name) reference of parent class;
Transformation between objects:
Upward Transformation: Fu f = new Son(); This is actually a polymorphic format
Downward transition: Son s = (Son)f; This transformation requires that the transformed class has an inheritance relationship with the parent class reference, and this class is used when creating polymorphism at the beginning.
Cat and dog cases:
class Animal{ public void eat(){ System.out.println("Eat something"); } } class Cat extends Animal{ @Override public void eat() { System.out.println("Cats eat fish");; } public void run(){ System.out.println("A cat catches a mouse"); } } class Dog extends Animal{ @Override public void eat() { System.out.println("Dogs eat bones"); } public void look(){ System.out.println("Dog watch"); } } public class Test1 { public static void main(String[] args) { Animal a=new Cat(); a.eat(); // a.run() cannot be executed here because polymorphism cannot access the unique methods of subclasses and requires downward Transformation: Cat c=(Cat)a;//Downward transformation c.eat(); c.run(); Animal a2=new Dog(); a2.eat();//Here, the same as above cannot be executed A2 Look(), which can only be executed after downward transformation Dog d=(Dog)a2; d.eat(); d.look(); } }
Abstract: a non-specific function is called an abstract method. If there is an abstract method in a class, the class is called an abstract class.
Characteristics of abstract classes:
1. Abstract classes and abstract methods need a keyword to modify: abstract
Modify a class and put it in front of class, for example: abstract class Animal {};
Modifies a method, which is usually placed after the permission modifier:
Define an abstract show method: public abstract void show();
2. Classes with abstract methods must be abstract classes, but abstract classes do not necessarily have abstract methods. Concrete classes cannot have abstract methods. Abstract classes can have abstract methods or methods with method bodies.
3. Abstract classes cannot be instantiated. How to call:
In the form of polymorphism, specific subclasses are used to instantiate and call methods. The professional term is abstract polymorphism
4. If the inheriting abstract class is a concrete subclass, you need to override all the abstract methods in the abstract class; If the inherited abstract class is also an abstract class, you can override the methods in the parent class or optionally.
abstract class Animal{ public abstract void eat(); public abstract void drink(); // Note: abstract methods have no method body {}, not even braces, and end directly with a semicolon // Java abstract methods cannot have a body } class Dog extends Animal{ @Override public void eat(){ System.out.println("Dogs eat meat"); } public void drink(){ System.out.println("Dogs drink water"); } } //Selective rewriting of abstract methods abstract class Demo{ public abstract void fun(); public abstract void fun2(); } abstract class Demo2 extends Demo{ public void fun(){ System.put.println("abstract class Demo2 Rewritten fun method"); } } public class AbstractDemo1 { public static void main(String[] args) { //Creating objects using polymorphic forms of concrete subclasses //Abstract polymorphic form Animal3 a = new Dog3(); a.eat(); a.drink(); } }
Characteristics of abstract class members:
Member variable: it can be either a variable or a constant
Construction method: there can be construction methods, but the abstract class cannot be instantiated. The construction method here is meaningless in theory, but in fact, the construction method provides a function to initialize the parent class in inheritance.
Member method: it can be an abstract method, but the specific subclass must override the method; It can also be not an abstract method to improve the reusability of code.
abstract class Animal{ int a=20; int b=100; Animal(){ System.out.println("This is Animal Nonparametric construction method in"); } public abstract void eat(); public abstract void show(){ System.out.println("Is not an abstract method in the parent class show"); } } class Cat extends Animal{ @Override public void eat() { System.out.println("Cats eat fish"); } } public class AbstractDemo2 { public static void main(String[] args) { Animal4 a = new Cat4(); System.out.println(a.a); System.out.println(a.b); a.eat(); a.show(); } }
What keywords can exist in abstract classes?
What keywords cannot the abstract keyword coexist with?
private; static; final;