In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to define and rewrite Java inheritance", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to define and rewrite Java inheritance" article.
1. Definition of inheritance
When multiple classes have the same properties and methods, you can choose to abstract these common parts into a parent class, and other subclasses can inherit the properties and behaviors of the parent class, so that the subclass object gets the same properties and methods as the parent class object.
The advantages of inheritance
(1) improve the reusability of code.
(2) there is a relationship between classes, which is the premise of polymorphism.
2. Format of inheritance
Through the extends keyword, you can declare that a subclass inherits a parent class and define the format:
Public class parent class name {/ / defines the format of the parent class.} public class subclass name extends parent class name {. / / define subclass format} 3, member variable duplicate name
In a parent-child class inheritance relationship, if the member variable has the same name, the subclass object is created and can be accessed in two ways:
(1) access member variables directly through subclass objects.
Priority will be given to whoever is on the left side of the equal sign, and if not, look up.
(2) access member variables indirectly through member methods.
If the member method belongs to, you will use it first, and if you don't have it, you will look up.
Public class Demo02ExtendsField {public static void main (String [] args) {Father father = new Father (); / / create parent object System.out.println (father.numFather); System.out.println ("="); Son son = new Son (); System.out.println (son.numFather); / / 10 System.out.println (son.numSon); System.out.println ("=") System.out.println (son.num); / / priority subclass 200System.out.println ("="); son.methodSon (); / / this method is defined in the subclass, giving priority to the subclass, instead of looking up for son.methodFather (); / / the method defined in the parent class}} public class Father {int numFather = 10; int num = 100 Public void methodFather () {System.out.println (num);}} public class Son extends Father {int numSon = 20; int num = 200; public void methodSon () {System.out.println (num);}}
Note: variables of the same name in the parent class can also be accessed through the super keyword; the this key is used to access variables of the same name in this class.
4. Member method renaming
If a member method with the same name appears in both the parent and child classes, Override is generally used for access.
Method rewriting: if the subclass has exactly the same member method as the parent class (the return type, method name, and parameter list are exactly the same), there will be an override effect, that is, rewriting. That is, the declaration will remain unchanged and be reimplemented.
Rewrite features: the method of whoever is created is preferred; it only looks up to the parent class, not down to the subclass.
Public class Demo04ExtendsMethod {public static void main (String [] args) {Son son = new Son (); son.methodFather (); son.methodSon (); System.out.println ("="); son.method ();}} public class Father {public void methodFather () {System.out.println ("parent method execution!") ;} public void method () {System.out.println ("parent method rename execution!") ;}} public class Son extends Father {public void methodSon () {System.out.println ("subclass method execution!") ;} @ Override public void method () {System.out.println ("subclass method execution with the same name!") ;}}
Considerations for method overrides:
(1) you must ensure that the method names and parameter lists of methods between parent and child classes are the same.
(2) the return data type of the subclass must be less than the return data type of the parent class.
(3) the permission of the subclass method must be greater than the permission modifier of the parent class.
(4) adding @ Override before method replication is used to detect whether it is a valid rewrite. But this is not necessary, but write but not write.
Extend:
(1) the Object class is the common highest parent class (ancestor class) of all classes.
(2) public > protected > (deafult) > private [(default) is not a keyword, but indicates that the permission modifier is left blank. ]
5. Rewritten application scenario public class Demo06Phone {public static void main (String [] args) {Phone phone = newPhone (); phone.call (); phone.send (); phone.show (); System.out.println ("="); NewPhone newPhone = new NewPhone (); newPhone.call (); newPhone.send (); newPhone.show () System.out.println ("=");}} public class Phone {public void call () {System.out.println ("call");} public void send () {System.out.println ("text message");} public void show () {System.out.println ("display number") }} public class NewPhone extends Phone {@ Override public void show () {super.show (); System.out.println ("Show name"); System.out.println ("Show avatar") }} the above is the content of this article on "how to define and rewrite Java inheritance". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please 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.