Fundamentals of Java (object-oriented feature 3: polymorphism)

conceptual analysis

Polymorphism is the most important concept in object-oriented, which is embodied in JAVA:
Object polymorphism: the reference of the parent class points to the object of the child class
It can be directly applied to abstract classes and interfaces

Java reference variables have two types: compile time type and runtime type. The compile time type is determined by the type used when declaring the variable, and the runtime type is determined by the object actually assigned to the variable. Abbreviation: when compiling, look at the left; When running, look to the right.
If compile time type and run-time type are inconsistent, object polymorphism occurs
In the case of polymorphism, "look at the left": look at the reference of the parent class (there are no child specific methods in the parent class)
"Look at the right": it looks at the objects of the subclass (actually running the methods of the subclass overriding the parent class)

Understanding polymorphism: it can be understood as multiple forms of a thing.

What is polymorphism: object polymorphism: the reference of the parent class points to the object of the child class (or the reference assigned to the parent class by the object of the child class)

Use of polymorphism: virtual method calls
With object polymorphism, we can only call the methods declared in the parent class at compile time, but at run time, we actually execute the methods of the child class overriding the parent class.
Summary: compile and look at the left; run and look at the right.

The premise of polymorphism: 1. The inheritance relationship of classes, 2. The rewriting of methods

Polymorphism of objects: only for methods, not for attributes (see the left for compilation and operation)

If the subclass overrides the parent method, it means that the method defined in the subclass completely covers the method with the same name in the parent class, and the system will not be able to transfer the method in the parent class to the subclass. See the left for compilation and the right for operation.

For instance variables, there is no such phenomenon. Even if an instance variable exactly the same as that of the parent class is defined in the subclass, it is still impossible for this instance variable to overwrite the instance variable defined in the parent class. See the left for compilation and operation.

Example 1

Person.java

package com.klvchen.java4;

public class Person {
    String name;
    int age;

        int id = 1001;
    
    public void eat() {
        System.out.println("people, having dinner");
    }
    
    public void walk() {
        System.out.println("People, walk");
    }

}

Man.java

package com.klvchen.java4;

public class Man extends Person {
    
    boolean isSmoking;
    
    public void earnMoney() {
        System.out.println("Men are responsible for making money to support their families");
    }
    
    public void eat() {
        System.out.println("Men eat more meat and grow muscles");
    }
    
    public void walk() {
        System.out.println("The man walked aggressively");
    }
}

Woman.java

package com.klvchen.java4;

public class Woman extends Person {
    
    boolean isBeauty;
    
    public void goShopping() {
        System.out.println("Women like shopping");
    }

    public void eat() {
        System.out.println("Women eat less in order to lose weight");
    }
    
    public void walk() {
        System.out.println("Women walk gracefully");
    }
}

PersonTest.java

package com.klvchen.java4;

public class PersonTest {
    public static void main(String[] args) {
        
        Person p1 = new Person();
        
        p1.eat();
        
        Man man = new Man();
        man.eat();
        man.age = 25;
        man.earnMoney();
        
        //**************************************
        System.out.println("***************************************");
        //Object polymorphism: the reference of the parent class points to the object of the child class(Or a reference assigned to the parent class by an object of a subclass)
        Person p2 = new Man();
        //When calling a method with the same name and parameter as the child parent class, the actual execution is the method of overriding the parent class by the child class --- Virtual method call
        p2.eat();
        p2.walk();
        
//        p2.earnMoney();

                System.out.println(p2.id);
    }
}

Operation results

Example 2

AnimalTest.java

package com.klvchen.java4;


public class AnimalTest {
    
    public static void main(String[] args) {
        
        AnimalTest test = new AnimalTest();
        test.func(new Dog());
        
        System.out.println("*************************************");
        
        test.func(new Cat());
    }
    
    public void func(Animal animal) {// Animal animal = new Dog();
        animal.eat();
        animal.shout();
    }

}


class Animal{
    
    public void eat() {
        System.out.println("animal, eat");
    }
    
    public void shout() {
        System.out.println("animal, call");
    }
}


class Dog extends Animal{
    public void eat() {
        System.out.println("Dogs eat bones");
    }
    
    public void shout() {
        System.out.println("Woof! Woof! Woof!");
    }
}

class Cat extends Animal{
    public void eat() {
        System.out.println("Cats eat fish");
    }
    
    public void shout() {
        System.out.println("Meow! Meow! Meow!");
    }
}

Operation results:

Tags: Java

Posted on Mon, 29 Nov 2021 02:24:37 -0500 by Duncan85