If you rewrite, you can also achieve code diversity.
After the subclass inherits the parent class, it does not need to rewrite the method of the parent class, which saves the amount of code.
If the usage conditions change and the method of the parent class cannot meet the usage requirements, then you need to rewrite the method and overwrite the method of the parent class.
You understand wrongly. Rewriting is the key to achieving polymorphism. Overloading just adds a few parameters and reuses method names. As for why rewriting is the key to achieving polymorphism, it is because object-oriented encourages interface-oriented programming, or abstraction-oriented programming. For example:
The parent class of a fruit has a printName method:
public class Fruit {
public void printName() {
System.out.println("Fruit");
}
}
Both apples and oranges can be subclasses of Fruit, but you can override the printName method during implementation to achieve different name outputs.
One of the benefits of inheritance is to reduce the amount of code. There is no need to write the same statement in every class. Direct inheritance is like the relationship between a father and his son. The son will inherit all the characteristics of the father, but the son can also develop on his own.
Rewrite the parent class method and define a special class with a template. They all look the same on the outside, but they are different on the inside. This is the advantage of interface-oriented programming
If you rewrite, you can also achieve code diversity.
After the subclass inherits the parent class, it does not need to rewrite the method of the parent class, which saves the amount of code.
If the usage conditions change and the method of the parent class cannot meet the usage requirements, then you need to rewrite the method and overwrite the method of the parent class.
Rewriting can work with many modes of Java.
You understand wrongly. Rewriting is the key to achieving polymorphism. Overloading just adds a few parameters and reuses method names. As for why rewriting is the key to achieving polymorphism, it is because object-oriented encourages interface-oriented programming, or abstraction-oriented programming. For example:
The parent class of a fruit has a printName method:
Both apples and oranges can be subclasses of Fruit, but you can override the printName method during implementation to achieve different name outputs.
One of the benefits of inheritance is to reduce the amount of code. There is no need to write the same statement in every class. Direct inheritance is like the relationship between a father and his son. The son will inherit all the characteristics of the father, but the son can also develop on his own.
Rewrite the parent class method and define a special class with a template. They all look the same on the outside, but they are different on the inside. This is the advantage of interface-oriented programming