Method overrides and overloads

1. Overload experience and rewriting (* java not be present in the parent class and subclass present in the same method name and returns the parameter value different types of overload)
rewrite rules:
  one of A, the rewrite rules:
    weight write method can not be more stringent than the level of access is restricted to override the method. (Not available from public to Private)
  B, the two rewrite rules:
    the argument list must be the same method and rewritten.
  Three C, the rewrite rule:
    return type must be the same as the return type of the overriding method. (Rewrite return type may be a father-son type relationship)
  D, four Rewrite Rules:
    Rewrite method does not throw new Exception or overridden method than to be declared abnormalities broader checked exceptions. But you can throw fewer, more limited or does not throw an exception.

Overload rules:
  refers to the same method on a class, a list of different parameters, return value may be different. (Overloaded subclass the parent class in the strict sense can be considered in the same class as the subclass inherits the parent class's method)

Java compile-time and run-time type and the type of heavy-duty and rewrite Methods:


  First, let's look at the Java run-time and compile-time type type. Type determines the compile-time type used by declaring the variable (also called static type) (previous type, also known as a reference point to the parent class subclass object), run-time type is determined by the actual target assigned to the variable. If the compile time type and run-time type of inconsistency,
  there will be so-called polymorphism. Because subclasses actually a special parent class java thus allows a subclass object directly assigned a parent class reference variable, without any type of conversion or transformation is called up automatically by the system.

  When the reference variable at compile time can only be called its compile-time type has a method, but it is executed run-time type method has run, therefore, when writing Java code, reference variables can only be called the variable is declared with the class contains Methods.
  And different methods, the property of an object not have the polymorphism. When accessing instance attributes it contains by reference variables, the system always tried to access class properties are defined at compile time, rather than attribute it runs defined.

For the Code: (person is a reference variable), that person can not call the student objects in the object's unique method (non-inherited from the Person object) and Student object in the property,
  Person person = new new Student ();
  will generate variables person, compile-time type of the variable is Person, and run-time type is Student.

For overloaded (overloaded), select the method call is determined at compile time, is static choice; and for covering (overridden) method of selection is dynamic, determined at runtime, based on the selected method is invoked where runtime type of the object.

About java program to compile and run the process
Java program created from the source file to run the program to go through two steps:
First, the source file by the compiler into bytecode (ByteCode) compilation phase is the JVM java editor compiled into .class files
process .java two classes running roughly divided into two steps: (1) class load (2) implementation of the class. It should be noted that it is important: JVM major program in the first run when the active use of the class, will be loaded and loaded only once

Guess you like

Origin www.cnblogs.com/nyhhd/p/12539271.html