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

What is the transformational use of inheritance in Java polymorphism

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

Share

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

This article introduces the knowledge of "what is the transformational use of inheritance in Java polymorphism". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

I. Preface

II. Transformation

Upward transformation

Downward transformation

3. Instanceof operator

The use of instanceof

The format of instanceof:

I. Preface

Earlier, we learned about the overview and use of polymorphism, and now let's learn about the rest of the transformation problems.

II. Transformation

? Upward transformation

? Downward transformation

Why is there a transition? as we mentioned earlier, the premise for the use of polymorphism is that there is a parent class reference pointing to the subclass object.

Another drawback of polymorphism is that subclass objects cannot be accessed. (downward transformation access can be used)

Upward transformation

? In fact, the reference of the parent class in the polymorphism to the subclass object is the downward transformation, and the downward transformation is the child to the parent.

Is to assign the space applied for by the subclass to the parent class. Form: fu f=new zi ()

Downward transformation

? The emergence of the downward transformation is to solve the problem of inaccessible subclass members in polymorphism. In fact, the downward transformation is essentially

It is a strong turn, from parent to child, and the parent object is transformed into a subclass object. Form: zi z = (zi) f

Let's look at an example:

In the fu class

Public class Fu {public void show () {System.out.println ("fu:1024 programmer's Day");} public void play () {System.out.println ("fu:1024 does not disappear");}}

In the zi class

Public class Zi extends Fu {@ Override public void show () {/ / override the show method super.show () in the fu class; System.out.println ("zi:1024 is coming");} public void game () {System.out.println ("zi: refreshing to write a blog");} public void juan () {System.out.println ("zi: full volume");}}

Create a new test class

Public class FuZiDemo {public static void main (String [] args) {Fu f=new Zi (); / upward transition Zi z = (Zi) f bank / downward transition z.show (); z.game (); z.juan (); z.play ();}}

By transforming up and down, we can access all members of the subclass parent class at this time

? In fact, although it is very convenient to create subclass objects directly, one of the conditions for us to implement polymorphism is that we have

The parent class reference points to the subclass object (the polymorphism of the interface is not considered for the time being)

Public class FuZiDemo {public static void main (String [] args) {Zi z=new Zi (); / / create objects directly through subclasses z.show (); z.game (); z.juan (); z.play ();}}

? From the above results, it can be seen that the upward transformation of the subclass object to the parent object does not need to display the transformation, while the parent object is transformed.

When you are a subclass object, you need to display forced type conversions.

Friendly Tip: there are still a few points to pay attention to in the downward transformation:

① must ensure that the parent class object is an instance of the subclass, otherwise the program reports an error.

② downwards transformed objects can access members and methods in the parent class, as well as members and methods in the subclass.

Public class Demo11 {public static void main (String [] args) {Animal a=new Dog (); Dog g = (Dog) a; Cat c = (Cat) a Whether it can succeed}}

? In the above code, the parent class object a refers to the subclass Dog object, not the Cat class object.

System to Dog type, not a forced conversion to Cat type, because the downward transformation must ensure that the parent class object is

An instance of the subclass, so the program cannot succeed, and a warning of a conversion exception appears at run time.

III. The use of the instanceof operator instanceof

? From the above, the downward transformation must make sure that the parent class object is an instance of the subclass, otherwise the program will be abnormal

The purpose of instanceof is to ensure that there are no exceptions in the downward transition. The instanceof operator is used at run time

Indicates whether the object is an instance of a particular class

The format of instanceof:

? Object name instanceof class name

Instanceof returns a Boolean value indicating whether the object is a member of this particular class or its subclass

An example. True is returned, and vice versa.

Public class Demo11 {public static void main (String [] args) {Animal a=new Dog (); if (an instanceof Dog) {Dog g = (Dog) a;} if (an instanceof Cat) {/ / judgment Cat c = (Cat) a;}

? So we can avoid some problems by using the instanceof operator before we make the transition down.

That's all for "what is the transformational use of inheritance in Java Polymorphism?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 0

*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