In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "Java class inheritance how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Java class inheritance how to use" this article.
The details are as follows:
Why inherit?
Observe the composition of the two classes
Extract the same attributes and methods
Pets are parents, dogs and goldfish are children. The subclass has the properties and methods of the parent class.
Inheritance definition
Is a technology that uses existing classes as the basis for building new classes.
Single inheritance: only one parent class.
A parent class can be called a base class or a superclass. Subclasses can be called derived classes.
Inheritance considerations
Subclasses can inherit members (properties and methods) in the parent class.
But you need to pay attention to:
Members of 1.private cannot inherit
two。 The subclass and the parent class are not in the same package, and members using default access rights cannot inherit
3. The constructor cannot inherit.
Inheritance principle
The design inheritance relationship should conform to the principle of Richter scale substitution. Is-a relation
The subclass is the parent class, which can completely replace the parent class, and the code has no effect.
Richter substitution principle (LSP): in a software system, subclass objects can replace all used parent objects, and the program behavior does not change.
Rewrite
In a subclass, the method name, parameters, and return value in the subclass are exactly the same as those in the parent class, so the subclass overrides the method in the parent class.
Only instance member methods can be overridden. Static member methods, static variables, instance member variables cannot be overridden and hidden.
The fundamental difference between rewriting and hiding
Rewriting can achieve polymorphism, hiding can not achieve polymorphism.
Rewrite considerations
1. The name must be exactly the same
two。 The parameter is the same as the subclass after the parent class is erased, or exactly the same as the subclass.
3. The return value type can be exactly the same. If it is a reference type, the return value type of the subclass is allowed to be a subclass of the parent class.
4. Access modifier subclasses cannot be more stringent than parent classes.
5. Exception handling cannot be larger than the parent class.
Why rewrite it?
1. The subclass modifies the functionality in the parent class.
two。 The subclass extends the functionality in the parent class.
Class parent phone {public void caller ID () {display phone number;}}
Class subclass mobile phones extends parent class mobile phones {public void caller ID () {display phone number; display person's name; display address;}}
Super
Represents the parent class scope
The super key is similar to this in that masked member variables or member methods are either made visible or used to refer to masked member variables and member methods. However, super is used in subclasses to access masked members of the immediate parent class, which is the nearest superclass above the class.
The usage of super
First, in the subclass constructor, to call the constructor of the parent class, using the "super (parameter list)" way, parameters are not required. Also note that the statement "super" can only be used in the first line of the subclass constructor body.
Second, when the local variable in the subclass method or the member variable of the subclass has the same name as the parent member variable, that is, when the subclass local variable overrides the parent member variable, use "super. Member variable name" to refer to the parent member variable. Of course, if the member variable of the parent class is not overridden, you can also use the "super. Member variable name" to refer to the parent member variable, but this is not necessary.
Third, when the member method of the subclass overrides the member method of the parent class, that is, the subclass and the parent class have exactly the same method definition (but the method body can be different), access the method of the parent class in the way of "super. Method name (parameter list)".
The above is all the content of the article "how to use Class inheritance in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.
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.