In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "Java inheritance application case analysis". In daily operation, I believe many people have doubts about Java inheritance application case analysis. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java inheritance application case analysis". Next, please follow the editor to study!
1. Code block initialization
Since the definition and use of code blocks have been introduced before, we are not going to repeat them here. What we are concerned about is the sequence of code blocks in the parent and subclasses at run time.
The first step is to add code blocks to the parent and subclasses, as follows:
/ / parent {System.out.println ("parent instance code block");} static {System.out.println ("parent static code block");} public Plant () {System.out.println ("parent class constructor");} / subclass {System.out.println ("subclass instance code block");} static {System.out.println ("subclass static code block") } public Tree () {System.out.println ("subclass constructor");}
We instantiate the subclass object in the main method and then take a look at the result:
The static code block is still executed first, then the instance code block and constructor of the parent class, and finally the instance code block and constructor of the subclass.
Since the static code block is executed only once, neither the parent class nor the subclass static code block will be executed if the subclass object is instantiated again.
2.protected keyword
There is also a protected left in the previous access modifier qualifier. Fill this hole here, and procted has one more permission in addition to the package access permission: subclasses in different packages can access members of the parent class.
Assuming that Plant and Tree are in two packages respectively, protected-decorated members of Plant can still use super to access them in Tree
One thing to note: the parent class must be decorated with public, otherwise the access permissions are not enough, and the two classes of different packages cannot form an inheritance relationship at all.
We have finished talking about the four access modifier qualifiers. Here is a summary:
Access range privatedefaultprotectedpublic same class √√√√ in the same package different classes × √√√ different packages subclass × × √√ non-subclass × × √ 3.final keywords in different packages
The final keyword is also used to modify classes, methods, and variables as follows:
Modifier variable: becomes constant and cannot be modified
Modification method: cannot be rewritten
Modifier class: cannot be inherited by other classes as a parent class
Inheritance methods allowed in 4.Java
Single inheritance: an inherits B
Multi-tier inheritance: an inherits BMIT B inherits C, C inherits D.
Different classes inherit the same parent class: an inherits BMager C also inherits B.
However, one class is not allowed to inherit multiple parent classes in Java (that is, multiple inheritance in C++).
At this point, the study on "Java inheritance application case analysis" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.