In class inheritance, subclasses can modify methods inherited from the parent class, which means that the subclass can create a method that has different functionality from the parent class method, but with the same name, return value type, and argument list.
If you define a method in a new class whose name, return value type, and argument list are exactly the same as in the parent class, the new method is called overwrite the old method.
Parameter lists are also called parameter signatures, including the type of the parameter, the number of arguments, and the order of the parameters, as long as there is a difference between the parameter list.
Overridden methods can only be called by super in subclasses.
Note: Overrides do not delete the methods in the parent class, but the instances of the child classes are hidden and are not used temporarily.
Take a look at the following example:
1 Public classdemo{2 Public Static voidMain (string[] args) {3Dog Mydog =NewDog ("Floral");4Mydog.say ();//An instance of a subclass calls a method in a subclass5 6Animal myanmial =NewAnimal ("Beibei");7Myanmial.say ();//An instance of the parent class calls a method in the parent class8 }9 }Ten classanimal{ One String name; A PublicAnimal (String name) { - This. Name =name; - } the - Public voidsay () { -System.out.println ("I am a small animal, my name is" + name + ", I will scream"); - } + } - classDogextendsanimal{ + //The construction method cannot be inherited and is called by Super () A PublicDog (String name) { at Super(name); - } - //overwrite Say () method - Public voidsay () { -System.out.println ("I am a puppy, my name is" + name + ", I will make a barking call.")); - } in}
Operation Result:
I am a puppy, my name is flower, I will make a barking call
I am a small animal, my name is Beibei, I will make a cry
Method overrides the principle:
- The return type, method name, and parameter list of the overridden method must be the same as the original method.
- The override method cannot be less accessible than the original method (that is, access permissions are not allowed to shrink).
- The override method cannot throw more exceptions than the original method.
- The overridden method cannot be a final type, because the final decorated method cannot be overridden.
- The overridden method cannot be private, otherwise only a new method is defined in its subclass and is not overwritten.
- The overridden method cannot be static. If the method in the parent class is static, and the method in the subclass is not static, the two method satisfies the overwrite condition in addition to this, and then a compilation error occurs, and vice versa. Even if the methods in the parent and child classes are static, and the overrides are met, the overwrite is still not occurring because the static method matches the static method and the reference type of the class at compile time.
Overloads of the method:
The Java method overloads are described earlier, and here again, the methods in the Java parent and subclass are overloaded, for example, one of the methods in the parent class is Func () {...}, one of the methods in the subclass is the func (int i) {...}, which forms the overload of the method.
Overrides and overloads are different:
- Method overrides require that the parameter list must be consistent, and the method overload requires that the argument list be inconsistent.
- Method overrides require that the return type be consistent, and the method overload does not require it.
- The method overrides methods that can only be used by subclasses to override the parent class, and the method overloads are used for all methods in the same class, including methods inherited from the parent class.
- Method overrides have special requirements for access to methods and exceptions thrown, and method overloading has no limitations in this regard.
- A method of a parent class can be overridden only once by a class, and a method can be overloaded multiple times in all classes.
Series Articles:
Java know how much (1) Language overview
Java know how much (2) virtual machine (JVM) and cross-platform principle
Java know how much (3) employment direction
Java know how much (4) the difference between J2SE, Java EE, J2ME
Java know how much (5) Build Java development environment
Java know how much (6) The first example of a program
Java knows how many (7) classes and objects
Java know how much (8) class library and its organizational structure
Java know how much (9) Import and Java class search path
Java know how much (10) data types and variables
Java know how much (11) data type conversions
Java know how many (12) operators
Java know how much (13) Process Control
Java know how many (14) arrays
Java know how much (15) string
Java know how much (StringBuffer) and Stringbuider
Java know how much (17) emphasize the programming style
Java know how many (18) classes are defined and instantiated
Java know how many (19) access modifiers (access control characters)
Java knows how many (20) variables are scoped
Java know how much (+) This keyword is detailed
Java know how many (22) method overloads
Java know how much (23) the basic run order of classes
Java know how much (24) packaging class, unpacking and packing detailed
Java know how much (25) More about Java package
Java know how much (26) claim rules for source files
Java know how much (27) the concept and implementation of inheritance
Java know how many super keywords
Java know how much (29) Overwrite and reload
Java know how much (29) Overwrite and reload