Resolves overrides and overloads of methods in Java inheritance

  • 2020-04-01 03:51:05
  • OfStack

Method coverage

In class inheritance, a subclass can modify a method inherited from a parent class, which means that a subclass can create a method with different functionality than a parent 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 parameter list are exactly the same as in the parent class, the new method is said to override the old method.

Parameter list is also called parameter signature, including the type of parameters, the number of parameters and the order of parameters, as long as there is a difference is called parameter list is different.

The overridden method can only be called through super in a subclass.

Note: overrides do not remove methods from parent classes, but are hidden from instances of subclasses and are not used for the time being.

Here's an example:


  public class Demo{
  public static void main(String[] args) {
  Dog myDog = new Dog(" Line circle ");
  myDog.say(); //An instance of a subclass invokes a method in the subclass
  Animal myAnmial = new Animal(" Line circle line ");
  myAnmial.say(); //An instance of the parent class invokes a method in the parent class
  }
  }
  class Animal{
  String name;
  public Animal(String name){
  this.name = name;
  }
  public void say(){
  System.out.println(" I am a small animal, my name is " + name + " I will make a cry ");
  }
  }
  class Dog extends Animal{
  //The constructor cannot be inherited and is called through super()
  public Dog(String name){
  super(name);
  }
  //Override the say() method
  public void say(){
  System.out.println(" I am a little dog, my name is " + name + " "I would bark ");
  }
  }

Operation results:


I am a little dog, my name is line circle, I can emit a bark
I am a small animal, my name is line circle line, I can make a cry

Principle of method coverage:

The return type, method name, and parameter list of the override method must be the same as that of the original method.

An override method cannot be less accessible than the original method (that is, the access permissions are not allowed to be reduced).

An override method cannot throw more exceptions than the original method.

Methods that are overwritten cannot be of a final type because methods that are final cannot be overwritten.

The overridden method cannot be private, or it simply has a new method defined in its subclass and is not overridden.

Overridden methods cannot be static. If the method in the parent class is static and the method in the subclass is not, but both methods satisfy the override condition except for this, a compilation error occurs. And vice versa. Even if the methods in both parent and subclass are static and satisfy the override condition, the override does not occur because the static method matches the static method with the class's reference type at compile time.

Method overloading:

Java method overloading was explained earlier, but it's important to note that methods in both Java parent and child classes participate in overloading. For example, one method in the parent class is func(){... }, one method in the subclass is func(int I){... }, constitutes an overload of the method.

The difference between overwriting and overloading:

Method overrides require that the parameter list be consistent, while method overloads require that the parameter list be inconsistent.

Method overrides require that return types be consistent, which method overloads do not.

Method overrides can only be used when a subclass overrides a method in a parent class, and method overrides are used for all methods in the same class (including methods inherited from the parent class).

Method overrides have special requirements on method access and exceptions thrown, and method overloads have no restrictions on this.

A method in a parent class can only be overridden by a subclass once, while a method can be overridden multiple times in all classes.


Related articles: