Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Java Polymorphism

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to understand Java polymorphism". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand Java polymorphism.

Object-oriented programming has three characteristics, namely, encapsulation, inheritance and polymorphism.

Encapsulation hides the internal implementation mechanism of the class, so that the internal structure of the class can be changed without affecting the user, while protecting the data.

Inheritance is to reuse parent code and to prepare for polymorphism. So what is polymorphism?

The rewriting, overloading and dynamic connection of the method constitute polymorphism. One of the reasons why Java introduces the concept of polymorphism is that it is different from C++ in terms of class inheritance, which allows for more inheritance, which does bring it a very powerful function, but the complex inheritance relationship has also brought more trouble to C++ developers. In order to avoid risks, Java only allows single inheritance, and there is an IS-A relationship between the derived class and the base class (that is, "cat" is a "animal").

Although this ensures the simplicity and clarity of the inheritance relationship, it is bound to have great functional limitations, so Java introduces the concept of polymorphism to make up for this deficiency. In addition, abstract classes and interfaces are also important means to solve the limitations of single inheritance. At the same time, polymorphism is the essence of object-oriented programming.

To understand polymorphism, we must first know what "upward transformation" is.

I define a subclass Cat, which inherits the Animal class, so the latter is the former is the parent class. I can pass.

Cat c = new Cat ()

Instantiating an Cat object is not difficult to understand. But when I define it this way:

Animal a = new Cat ()

What does that mean?

Quite simply, it means that I have defined a reference of Animal type to an object of the newly created Cat type. Because Cat inherits from its parent class Animal, references of type Animal can point to objects of type Cat. So what's the point of doing this? Because the subclass is an improvement and extension of the parent class, the general subclass is more powerful than the parent class, and the attribute is more unique than the parent class.

Defining a reference to a parent class type points to an object of a subclass that not only uses the powerful capabilities of the subclass, but also extracts the commonness of the parent class. Therefore, the reference of the parent class type can call all the properties and methods defined in the parent class, but it can do nothing about the methods defined in the subclass but not in the parent class; at the same time, a method in the parent class can be called by the reference of the parent class only if it is defined in the parent class and is not overridden in the child class For a method defined in the parent class, if the method is overridden in the subclass, the reference to the parent type will call the method in the subclass, which is dynamic join.

Look at the following program:

Class Father {public void func1 () {func2 ();} / this is the func2 () method in the parent class, because the method is overridden in the following subclass / / so when called in a reference to the parent class type, the method will no longer be valid / / instead, the func2 () method public void func2 () {System.out.println ("AAA") overridden in the subclass will be called. }} class Child extends Father {/ / func1 (int I) is an overload on the func1 () method / / since this method is not defined in the parent class, it cannot be called by a reference of the parent class type / / so child.func1 (68) is incorrect in the following main method public void func1 (int I) {System.out.println ("BBB") } / / func2 () overrides the func2 () method in the parent class Father / / if the func2 () method is called in a reference to the parent class type, then it must be the method public void func2 () {System.out.println ("CCC") overridden in the subclass;}} public class PolymorphismTest {public static void main (String [] args) {Father child = new Child (); child.func1 (); / / what will be the printed result? }}

The above program is a typical example of polymorphism. The subclass Child inherits the parent class Father and overrides the parent class's func1 () method and overrides the parent class's func2 () method. The overloaded func1 (int I) and func1 () are no longer the same method, and since there is no func1 (int I) in the parent class, the reference child of the parent type cannot call the func1 (int I) method. While the subclass overrides the func2 () method, the reference child of the parent type will call the overridden func2 () in the subclass when the method is called.

So what kind of results will the program print?

Obviously, it should be "CCC".

For polymorphism, it can be summarized as follows:

Use the reference of the parent class type to point to the object of the subclass

This reference can only call methods and variables defined in the parent class

3. If a method in the parent class is overridden in the subclass, the method in the subclass will be called when the method is called; (dynamic connection, dynamic invocation)

Fourth, variables cannot be overridden (overridden). The concept of "rewriting" is only for methods. If variables in the parent class are "overridden" in a subclass, an error will be reported at compile time.

At this point, I believe you have a deeper understanding of "how to understand Java polymorphism". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 218

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report