Ii. Java Object-oriented (8) _ Inheritance thought-method coverage

Source: Internet
Author: User
Tags instance method

2018-05-01

method Overrides (Overrides)

Override (Overwrite):

Overwrite resolved issue: when a parent class's behavior (method) does not conform to the specific characteristics of a subclass, the parent class needs to redefine the parent class method and override the method body.

Note: only the method has the concept of overwrite, the field does not.

Method overrides the principle:

  1. The instance method signature must be the same. (Method signature = Method Name + method parameter list)

2. A subclass method cannot return a data type that is larger than the parent class method, the return value type of the subclass method must be the same as the return value type of the parent class method, or the return value type of the subclass method is a subclass of the return value type of the parent class method.

(Subclasses can return more specific data types, such as the parent class returning an object, the subclass can return a string)

3. The subclass method cannot be lower than the access permission of the parent class method

4. The subclass method cannot throw more exceptions than the parent class method, and the subclass method throws an exception that must be the same as the exception thrown by the parent class method, or the exception class thrown by the subclass method is a subclass of the exception class thrown by the parent class method.

5. Method overrides exist only between the subclass and the parent class, including the direct parent class and the indirect parent class. Methods in the same class can only be overloaded and cannot be overwritten.

Recommendation: Copy the Parent-class method directly into the subclass, and then rewrite its method body.

Method Coverage: Write the @override label before the method.

If the overwrite succeeds, the compilation passes, or the compilation fails.

code example:

1 //Birds2 classBird3 {4      Public voidFly () {5SYSTEM.OUT.PRINTLN ("Free Flight");6     }7 }8 //Penguin9 Ten classPenguinextendsBird One { A @override -      Public voidFly () { -System.out.println ("The angel of the Broken Wing"); the     } - } -  -  +  - //method Overrides + classOverridedemo A { at      Public Static voidMain (string[] args) -     { -         //Create a Penguin object -Penguin p =NewPenguin (); -         //call the Fly method - p.fly (); in     } -}

Output Result:

Reference blog:

http://ihyperwin.iteye.com/blog/1553609

--------------------------------------------------------------------------------------------------------------- --------

The difference between method overrides and method overloads:

Override: overwrite (override)

Overload: Heavy Duty

In fact, they have nothing to do with it.

method overload definition: defining multiple methods with the same name in a class requires a different list of parameters between the methods.

method Overloading: A method that completes a set of similar tasks but differs in the number of types or parameters of a parameter. (Methods that block the same function vary in method names due to different parameters)

method overloads are called: The Java compiler can choose an appropriate method by examining the parameter types and the number of methods called.

method overload judgment: Two The same different

Two identical: Same class, method name is same

One difference: The parameter list of the method is inconsistent (parameter type, number of arguments, parameter order)

Note: method overloads and method return value types are not independent, only common methods that require the same functionality return value types consistently

The parameter list is independent of the parameter name, that is, the method overload is independent of the formal parameters

The Java compiler can choose an appropriate method by examining the parameter types and the number of methods called.

Ii. Java Object-oriented (8) _ Inheritance thought-method coverage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.