Java method Overloads, Method overrides (Overrides), inheritance, and other details note

Source: Internet
Author: User
Tags instance method

1. Specific specifications for method overloads (methods Overload)

If the method name of two methods is the same, but the parameters are inconsistent, then one method is the overload of the other.

I. The method name must be the same. Two. The parameter table of the method must be different, including the type or number of parameters, to distinguish between different method bodies. 1. If the number of parameters is different, regardless of its parameter type! 2. If the number of arguments is the same, then the order of the parameters must be different. Three. The method's return type, modifiers can be the same, or different. Four. The main method can also be overloaded with method overloads: You can pass in different parameters in the same way to achieve the desired result 2. Method Inheritance :Use the Extends keyword one method to inherit another method, and inherit only one class directly. I. When the sub class and the base class are in the same package, the Sub class inherits the public/protected/default-level variable method in the base class

Note: Inherit public/protected level variables and methods at different packages.

The role of method inheritance can improve the reusability and security of code

Access rights in different packages in 3.Java

1,public 2,protected 3, (default) 4,private
1 Any class can access
2 All classes in the same package can be accessed, and subclasses of the class
3 All classes in the same package
4 can only be accessed in this class

3. Methods override (Method override): If you define a method in a subclass whose name, return type, and parameter signature match exactly the name, return type, and parameter signature of a method in the parent class, you can say that the method of the subclass overrides the method of the parent class.
    • The method name return type of the subclass and the parameter signature must be consistent with the parent class
    • Subclass methods cannot narrow the access rights of a parent class method
    • Subclass methods cannot throw more exceptions than the parent class method
    • Method overrides exist only between subclasses and parent classes and can only be overloaded in the same class
    • Static methods of a parent class cannot be overridden by a quilt class as a non-static method
    • A subclass can be defined in a static method with the same name as a static method of the parent class to hide the static method of the parent class (which satisfies the overwrite constraint) in the subclass.
    • And the Java virtual machine binds the static method to its owning class, and binds the instance method to the instance it belongs to.
    • A non-static method of a parent class cannot overwrite a quilt class as a static method
    • Private methods of the parent class cannot be overridden by the quilt class
    • The abstract method of the parent class can be overridden in two ways by the quilt class (i.e. implementation and overwrite)
    • Non-abstract methods of the parent class can be overridden as abstract methods
4. Super Keyword:Both the super and the This keyword can be used to override the default scope of the Java language, making the masked method or variable visible.
    • member variables and methods for the parent class use Super Access compilation error for private
    • In the construction method of the class, the constructor of the class's parent class is called through the super statement
    • To access the masked methods and properties of the parent class in a subclass of class
    • The Super keyword can only be used within a constructor or instance method, and super is not available in static and static code blocks
5, polymorphic:
    • For a variable of a reference type, the Java compiler processes it according to its declared type
    • For a variable of a reference type, the runtime Java Virtual machine processes the object that it actually refers to
    • In the runtime environment, when you access the methods and properties of the referenced object by referencing the type variable, the Java virtual machine takes the following binding rules
1) The method binding of the instance method to the object that the reference variable actually refers to, which is a dynamic binding 2) a method binding of a static method to a type declared by a reference variable, a static binding 3) a member variable (including static and instance variables) that is bound to a member variable of the type declared by the reference variable, belongs to the static State binding Remarks: On the Transition object
The above transformation objects have the following characteristics:
1) on the transformed object cannot manipulate the new member variables of the subclass (lost this part of the property), can not use the subclass new method (lost some features).
2) on a transformed object you can manipulate member variables that are inherited or hidden by subclasses, or you can use a subclass-inherited or overridden method.
3) When a transformation object operates on a method that inherits or overrides a subclass, it notifies the corresponding subclass object to invoke those methods. Therefore, if the subclass overrides a method of the parent class, the object's top-transforming object calls this method, which must have been called.
4) The object of the object can be converted to a sub-class objects, and then the subclass object has all the properties and functions of the subclass. (That is, to summarize) is the parent class's variables and methods, the subclass can only override (overwrite) the method of the parent class, because it is dynamically bound Function: polymorphic has compile-time polymorphism and run-time polymorphism.
The first is implemented by method overloading, and the second is implemented by method overrides (subclasses override parent-class methods).
The first is that we call the method is not to distinguish between parameter types, the program will automatically execute the corresponding method, such as: addition operation, you can add int, can be double add, both are the same method name.
The second is dynamic binding, where a parent class reference is used to point to a subclass object, and then a method in a parent class is called, and different subclasses exhibit different results. This role is very good extensibility, played online games should know that there are different roles in the game, they have a parent class, they do the same action when the effect will not be the same, such as running, the wizard's run with the warrior's run will not be the same, this is the two have covered the father of the running method, each has its own reality, Show polymorphism. If one day you want to add a role, just write a class to inherit the parent class, overwriting the running method is OK, the other code does not change, so maintainability is also very good.

6, the advantages and disadvantages of inheritance and the use of principles:
    • The number of integrations cannot be too many levels
    • The upper level of the integration number is the abstraction layer
(1) The same properties and methods are defined for the lower subclass, and implemented as default as possible, thus improving reusability (2) Represents the interface of the system and describes the services that the system can provide
    • The biggest weakness of inheritance: breaking packages
    • Specially designed for inherited classes
(1) These classes must be provided with good documentation (2) to encapsulate the implementation details of the parent class as much as possible, defining the attributes and methods representing time details as private types (3) If some implementation details must be accessed by the quilt class, defined as the protected type (4) Define methods that do not allow subclass overrides to be final type (5) The construction method of the parent class does not allow the invocation of a method that can be overridden by a quilt class (6) If some classes are not designed for inheritance, it is unsafe to inherit it arbitrarily.

Java method Overloads, Method overrides (Overrides), inheritance, and other details note

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.