The two days of learning polymorphism, nature involves the method of rewriting. In the author's blind use of code toss the process, found that the error type mentioned "This static method cannot hide the instance method from Fu" (such as), so that in addition to cover (rewrite) There are hidden this matter, a variety of search information, Finally found a more detailed article, to solve a lot of my doubts. Forward, you may have to return to see later.
1. Compile-time type and run-time type
There are two types of reference variables for Java, one for compile-time types and one for run-time types
compile-time type : determined by the type used when declaring the variable
run-time type : determined by the type of object to which the variable points
If the compile-time type and run-time type are inconsistent, so-called polymorphism occurs. Because subclasses are actually a special kind of parent class, Java allows a subclass object to be assigned directly to a parent class reference variable, without any type conversion, or as an upward transformation, which is automatically done by the system.
such as Father f = new Son (); Son is a subclass of father.
The reference variable F will appear when the compile-time type and run-time type are inconsistent at compile time when the runtime of the father type is the son type
When a variable's compile-time type and run-time type are inconsistent, the value of the instance variable is determined by the type that declares the variable when it accesses an instance of the object it refers to.
When you access a method of an object that it refers to by a variable, the behavior of the method is determined by the actual type of the object being referenced.
2. Hide and overwrite
Overwrite : Subclasses override the method of the parent class, requiring the method name to be exactly the same as the parameter type (the argument cannot be a subclass), the return value and the exception are smaller or the same as the parent class (that is, the subclass of the parent class), and the access adornment transmitting parent class is large or identical.
Overrides are for instance methods.
methods cannot cross-cover : The subclass instance method cannot overwrite the static method of the parent class;
A static method of a subclass cannot overwrite an instance method of the parent class (Compile times error)
Hide : The parent class and subclass have properties or methods with the same name (the method hides only one form, that is, the parent class and the subclass have the same static method), the parent class has the same name property or the method form is missing, actually still exists.
Hiding is for static methods and member variables (static variables and instance variables).
(1) When a hidden event occurs, the declared type is a class that invokes the property or method of the corresponding class without dynamic binding
(2) Attributes can only be hidden and cannot be overwritten
(3) variable can be cross-hidden: subclass instance variable/Static variable can hide instance/static variable of parent class
3. The difference between hiding and covering
(1) Properties that are hidden are accessed by attributes in the parent class after the child class is cast to the parent class
Subclass to access the parent class's properties using the Super keyword when no casts are cast
(2) Overridden method that is called by the subclass itself after the subclass is coerced to a parent class
Subclass if you want to access the methods of the parent class, you can use the Super keyword
Related articles:
An overview of a deeper approach to Java overriding equals
Introduction to Overloading (overload) and overwrite (override) in PHP and Java