Home > Java > Javagetting Started > body text

How to understand java polymorphism

青灯夜游
Release: 2021-04-13 17:55:39
Original
6503 people have browsed it

In Java, polymorphism is the ability of the same behavior to have multiple different manifestations or forms; polymorphism is the same interface, using different instances to perform different operations. The advantages of polymorphism: 1. Eliminate coupling relationships between types; 2. Replacement; 3. Extensibility; 4. Interface; 5. Flexibility; 6. Simplification.

How to understand java polymorphism

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

Polymorphism Overview

1. Polymorphism is the third major feature of object-oriented after encapsulation and inheritance.

2. PolymorphismPractical meaningUnderstanding:

  • Real things often take on multiple forms, such as students , students are a kind of people, then a specific classmate Zhang San is both a student and a person, that is, there are two forms.                                                              The language of objects can also describe multiple forms of a thing. If the Student class inherits the Person class, a Student object is both a Student and a Person.

  • 3. Polymorphism is reflected in

    The parent class reference variable can point to the subclass object

    .
4. Prerequisite: There must be a child-parent class relationship.

Note: When calling a method using a polymorphic parent class reference variable, the rewritten method of the subclass will be called.

5. Definition and usage format of polymorphismDefinition format:Parent class type variable name=new subclass type();

6. Understand:

Polymorphism is the ability of the same behavior to have multiple different manifestations or forms.

Polymorphism is the same interface, using different instances to perform different operations.

7. Advantages of polymorphism

Eliminate the coupling relationship between types

  • Replaceability

  • Extensibility

  • Interface

  • ##Flexibility
  • Simplification
  • 8. Three necessary conditions for the existence of polymorphism
  • Inheritance

    Rewrite
  • The parent class reference points to the child class object:
  • Parent p = new Child();

  • # #Characteristics of members in polymorphism

    1. Polymorphic member variables: Look on the left when compiling and running
  • Fu f=new Zi();
    System.out.println(f.num);//f是Fu中的值,只能取到父中的值
    Copy after login
2. Polymorphic member methods: Look on the left when compiling and right when running

Fu f1=new Zi();
System.out.println(f1.show());//f1的门面类型是Fu,但实际类型是Zi,所以调用的是重写后的方法。
Copy after login
instanceof keyword

Function: Used to determine whether an object belongs to a certain data type.

Note: The return type is Boolean

Use case:
        Fu f1=new Zi();
        Fu f2=new Son();
        if(f1 instanceof Zi){
            System.out.println("f1是Zi的类型");
        }
        else{
            System.out.println("f1是Son的类型");
        }
Copy after login

Polymorphic transformation

Polymorphism The transformation is divided into two types: upward transformation and downward transformation.

Upward transformation: polymorphism itself is a process of upward transformation

Usage format: parent class type variable name = new child Class type();
  • Applicable scenarios: When there is no need to face subclass types, the corresponding operations can be completed by improving scalability or using the functions of the parent class.
  • Downward transformation: A subclass object that has been upwardly transformed can use the format of forced type conversion to convert the parent class reference type to the subclass reference type

Usage format: Subclass type variable name = (subclass type) Parent class type variable;

Applicable scenarios: When you want to use the unique functions of the subclass.
  • Polymorphic case:

Example 1: (

To understand polymorphism, you can focus on this case

)
package day0524;

public class demo04 {
    public static void main(String[] args) {
        People p=new Stu();
        p.eat();
        //调用特有的方法
        Stu s=(Stu)p;
        s.study();
        //((Stu) p).study();
    }
}
class People{
    public void eat(){
        System.out.println("吃饭");
    }
}
class Stu extends People{
    @Override
    public void eat(){
        System.out.println("吃水煮肉片");
    }
    public void study(){
        System.out.println("好好学习");
    }
}
class Teachers extends People{
    @Override
    public void eat(){
        System.out.println("吃樱桃");
    }
    public void teach(){
        System.out.println("认真授课");
    }
}
Copy after login

Example 2:

What is the result of running the project?

package day0524;
public class demo1 {
    public static void main(String[] args) {
        A a=new A();
        a.show();
        B b=new B();
        b.show();
    }
}
class A{
    public void show(){
        show2();
    }
    public void show2(){
        System.out.println("A");
    }
}
class B extends A{
    public void show2(){
        System.out.println("B");
    }
}
class C extends B{
    public void show(){
        super.show();
    }
    public void show2(){
        System.out.println("C");
    }
}
Copy after login
Answer: A B Related video tutorial recommendations:

Java video tutorial

The above is the detailed content of How to understand java polymorphism. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template