Type conversion and polymorphism in java basic course

We used classes to create new types and inheritance to facilitate the process of creating classes. In this lecture, I will delve into types and introduce the concept of polymorphism.

Type check

Any variables and references in Java can only be used after type declaration. We've seen object data, class data, method parameters, method return values, and automatic variables inside methods before. They all need to declare their types. Java is a strongly typing language that checks types. If we use the wrong type, it will cause an error.

The type is inconsistent, and the seller is invalid

For example, in the following test class, we assign a cup class object to the apoerson class reference:

Javac will return:

Basic type conversion

Java can type convert variables of basic types. Different basic types have different lengths and storage ranges. If we convert from a high-precision type to a low-precision type, such as float to int, we may lose information. Such a conversion is called narrowing conversion. In this case, we need to display the declaration type conversion, such as:

If we convert from low precision type to high precision type, there is no concern of information loss. Such a transformation is called widening conversion. We do not need to display the required type conversion. Java can automatically:

Basic type conversion

Upcast and polymorphism

In Java, references can also be type converted, but there are limitations.

We can convert a derived class reference to its base class reference, which is called upcast or loose conversion. The following brokencup class inherits from the cup class and overrides the original addwater() and drinkwater() methods in the cup class:

Program running results:

As you can see above, without any display instructions, we assign the derived class reference akokencup to its base class reference acup. Type conversion will be done automatically by Java.

We then invoked the addWater () method of aCup (which we declare it is Cup type). Although acup is a reference of type cup, it actually calls the addwater () method of brokencup! In other words, even if we pass upcast and relax the referenced type as its base class, Java can still correctly identify the type of the object itself and call the correct method. Java can identify the real type of object according to the current situation, which is called polymorphism. Polymorphism is an important aspect of object-oriented.

Polymorphism is not only a mechanism supported by Java, but also an important concept of object-oriented. This raises a taxonomic problem, that is, the subclass object is actually "the parent object". For example, a bird is also an animal; A car must also be a means of transportation. Java tells us that a derived class object can be used as a base class object, and Java will handle this situation correctly.

For example, the following inheritance relationship:

We can say drink water with a cup. In fact, the specific meaning of drinking water will change greatly in derived classes. For example, drinking water with a straw and drinking water from a broken cup will be very different, although we all talk about "drinking water" in the abstract. Of course, we can program separately for each derived class and call different Drinkwater methods. However, as programmers, we can program the cup and call the Drinkwater () method of cup, no matter what kind of derived cup the cup is. Java will call the appropriate correct method, as we saw in the above program.

For a more meaningful example, we add a drink () method to the human class, which takes a cup object and an integer as parameters. An integer indicates the amount of water to drink:

Program running results:

In the definition of drink () of human class, the first parameter is required to be a reference of cup type. However, in practical application (test class), the brokencup of the cup is derived from the class object. This is actually the upward transformation of hiscup, called the cup class, and passed to the drink () method. In the method, we call the Drinkwater () method. Java finds that this object is actually a brokencup object, so it actually calls the corresponding method of brokencup.

downcast

We can downcast a base class reference into a reference of a derived class, but the object pointed to by the base class reference is already the derived class object to be downcast. For example, the above hiscup can be transformed upward into a cup class reference, and then downward into a brokencup class reference.

Object type

In Java, all classes actually have a common inheritance ancestor, that is, the object class. The object class provides some methods, such as toString (). We can override these methods in our own class definition.

Object: ancestor

We can write a program that operates on object objects, and we can pass any object to the program through upcast.

I'll dig into the object class later.

(the implementation of polymorphism depends on the support of RTTI. I will go into it later.)

summary

Basic type conversion

polymorphism

downcast

Object

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>