Home>Article>Java> Java Basics: Understanding and Application of Polymorphism

Java Basics: Understanding and Application of Polymorphism

Robert De Niro
Robert De Niro forward
2019-08-13 16:52:25 2677browse

Java Basics: Understanding and Application of Polymorphism

Overview of polymorphism

1. Polymorphism is the third major object-oriented method after encapsulation and inheritance. characteristic.

2. Understanding the meaning of polymorphic reality:

Real things often take on multiple forms, such as students. Students are a type of human being. A specific classmate Zhang San is both a student and a human being, that is, he appears in two forms.

As an object-oriented language, Java 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 fact that parent class reference variables can point to subclass objects.

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

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

5. Polymorphic definition and usage format

Definition format: parent class type variable name=new subclass type();

Characteristics of members in polymorphism

1. Polymorphic member variables: Look at the left side when compiling and running

Fu f=new Zi();

System.out.println(f.num);//f is the value in Fu, and can only get the value in the parent

2. Polymorphic member method: look on the left when compiling, and on the right when running

Fu f1=new Zi();

System.out.println(f1.show());//The facade type of f1 is Fu, but the actual type is Zi, so The overridden method is called.

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的类型"); }

Polymorphic transformation

Polymorphic transformation points There are two types of upward transformation and downward transformation

Upward transformation: polymorphism itself is a process of upward transformation

Usage format: parent class type variable name=new subclass 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 forced type conversion format to convert the parent class reference type to the subclass reference type.

Using format: subclass type Variable name = (subclass type) Variable of parent class type;

Applicable scenario: When you want to use the unique functions of the subclass.

Polymorphic case:

Example 1:

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("认真授课"); } }

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"); } }

The above is the detailed content of Java Basics: Understanding and Application of Polymorphism. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete