Application of method overrides
When a subclass needs the functionality of a parent class, and the body subclass of the feature has its own content, you can override the methods in the parent class, which is to follow the functionality of the parent class and define the content that is unique to the subclass.
1 /*2 method Overrides: A method in a subclass that is exactly the same as a method declaration in the parent class (the same method name, parameter list, and return value type) is also referred to as method overrides, method replication. 3 4 method Overloading: The method name that appears in this class is different from the parameter list, regardless of the return value. 5 6 when the member method name of the subclass is the same as the member method name in the parent class, how does this access? 7 to access the lookup order of a member method through a subclass object:8 1. Find in the member method of the subclass and use it. 9 2. Find in the member method of the parent class and use it. Ten 3. Can not find, the error. One A methods to override the application: - you can override a method in a parent class when the subclass needs the functionality of the parent class, and the body subclass of the feature has its own unique content . - In this way, the function of the parent class is inherited, and the subclass's own content is defined. the - Case: - A: Define a mobile phone class. - B: Through research, I invented a new mobile phone, the role of the phone after the call, you can listen to the weather forecast. + according to our basic design, we write out the code. - but what? We also found that the new phone should be a mobile phone, so it should inherit from the phone. + in fact, the design of this time is not the best. A because cell phone call function, is the handset itself has the most basic function. at So, my new phone is not available for this feature. Sub-class commented out the function of the call, - However, at this time, the phone function is gone. This is not good. - in the end, this feature is still to be added. - since it inherits from the phone class, we use the function of the parent class directly. - So, how do you use the functionality of the parent class? Can be called with the Super keyword. - */ in - classPhone { to Public voidCall (String name) { +SYSTEM.OUT.PRINTLN ("Call" +name+ "); - } the } * $ classNewphoneextendsPhone {Panax Notoginseng Public voidCall (String name) { - //System.out.println ("Give" +name+ "call"); the Super. Call (name);//inherit the functionality of the parent class +System.out.println ("Can listen to the weather Forecast");//subclasses of their own unique functions. A } the } + - classExtendsDemo9 { $ Public Static voidMain (string[] args) { $Newphone NP =NewNewphone (); -Np.call ("Brigitte"); - } the}
Application of method rewriting in Java