When writing a subclass, we can still declare member variables. A special case is that the name of the declared member variable is the same as the name of the member variable inherited from the parent class, and the declared type can be different. In this case , the subclass will hide the inherited member variables.
The characteristics of subclasses hiding inherited member variables are as follows:
(1) Subclass objects and methods defined by the subclass themselves operate member variables with the same name as the parent class, which refers to the member variable redeclared by the subclass.
(2) Subclass objects can still call methods inherited from the parent class to operate member variables hidden by the subclass. That is to say, the member variables operated by methods inherited by the subclass must be member variables inherited or hidden by the subclass.
Notice:
Methods inherited from subclasses can only operate on inherited and hidden member variables of subclasses. A method newly defined by a subclass can operate on member variables inherited by the subclass and newly declared by the subclass, but it cannot operate on member variables hidden by the subclass.
Subclasses can hide inherited methods by overriding them. Method overriding is also called method overriding. If a subclass can inherit a method from the parent class, then the subclass has the right to override this method. Method overriding refers to defining a method in a subclass. The type of this method is consistent with the type of the method of the parent class or a subtype of the type of the method of the parent class, and the name of the method, the number of parameters, the type of the parameters and The methods of the parent class are exactly the same. The method defined in this way by a subclass is called a method overridden by the subclass and is not a new method.
Subclasses can hide inherited methods through method rewriting, and change the state and behavior of the parent class to their own. If the parent class's method f() can be inherited by the child class, the child class has the right to override f(). Once the child class overrides the parent class's method f(), the inherited method f() is hidden. Then the child class has the right to override f(). When a class object calls method f(), it must call the overridden method f(). If the subclass does not override, but inherits the method f() of the parent class, then of course the object created by the subclass can call the f() method, but the behavior produced by the method f() is the same as that of the parent class.
The overridden method can not only operate inherited member variables and call inherited methods, but also can operate newly declared member variables of the subclass and call other newly defined methods, but it cannot operate the member variables and methods hidden by the subclass. If a subclass wants to use hidden methods or member variables, it must use the super keyword.
Notice:
When overriding a method of a parent class, you are not allowed to reduce the access rights of the method, but you can increase the access rights. The order of access restriction modifiers from high to low access rights is: public, protected, friendly, private. For example: a subclass overrides method f of the parent class. The access permission of this method in the parent class is the protected level. When the subclass overrides, the level is not allowed to be lower than protected.