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 Java inheritance and how to implement it

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

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you what Java inheritance is and how to achieve the relevant knowledge, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

1. What is inheritance?

The so-called inheritance is to extract the commonness of the class, and then realize the reuse of the code.

The inherited keyword is extends

Now define a class Tree, which contains the name, origin and family of the tree. The method is to print the morphological characteristics of the tree, as follows:

Class Tree {String name; String source; String genu; public void trait () {}}

Except for the method trait, all the members of the above class are duplicated, so we define another class Plant to extract these duplicate members, as follows:

Class Plant {String name; String source; String genu;}

Then let Tree inherit Plant:

Class Plant {String name; String source; String genu;} class Tree extends Plant {public void trait () {}}

Where Tree is a subclass / derived class and Plant is a parent / base class

So if there are really Plant members in Tree, you might as well instantiate an object to see:

The significance of instantiation is not only to realize code reuse, but also to implement polymorphism, which will be discussed in later articles.

two。 Details of inheritance

After understanding what inheritance is, we will have an in-depth understanding of some details of inheritance.

2.1super keyword

Super is used in non-static methods. Its main function is to access the members of the parent class in the subclass. It can act on the member variables, member methods and constructors of the subclass. The use of the constructor is explained in the subclass constructor.

Duplicate names of subclass members and parent class members

It is easy to prove that members of subclasses and members of parent classes with duplicate names give priority to members of subclasses, as shown in the figure:

We now add the following statement to the trait of Treed:

Public void trait () {System.out.println (super.name+ "can be up to 30 meters high");}

The result is shown in the figure:

If it is a method overname, it will constitute overloading or overriding, and rewriting is polymorphic content. Here is a brief introduction, that is, the return value, method name and parameters of the parent class and subclass methods are all the same. If it is overloaded, you can determine whose method is accessed by the parameters. If overloaded, add "super. Method name" to the method to access the parent class method.

2.2 Construction methods of subclasses

Because the subclass inherits the members of the parent class, the first thing to do when writing the subclass constructor is to call the parent constructor. Super is still used here, and we write a constructor with three parameters in Plant:

Public Plant (String name, String source, String genu) {this.name = name; this.source = source; this.genu = genu;}

The subclass is constructed as follows (assuming that the subclass does not have its own member variables):

Public Tree (String name, String source, String genu) {super (name, source, genu);}

One thing to note: super () can only appear in the first line of the constructor.

As I said before when writing the constructor, the compiler will provide a constructor without parameters even if the user does not write the constructor, but the first line of the subclass constructor will add super ().

The differences between 2.3super and this can only be used to access non-static member methods and member variables in non-static methods of a class.

This is a reference to the current object, and super is equivalent to referring to the part of the member that inherits the parent class in the subclass.

Use in a constructor can only be placed on the first line (so super () and this () cannot appear at the same time in the constructor) this is a hidden parameter of a non-static member method, super is not

These are all the contents of the article "what is Java inheritance and how to implement it?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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