Java Override and Overload

Override

Rewriting is a subclass that rewrites the implementation of the method that allows access to the parent class, and neither the return value nor the formal parameters can be changed. The shell is unchanged, the core rewrite!
The benefit of rewriting is that subclasses can define their own behavior as needed. In other words, subclasses can implement parent methods as needed.
Overriding methods cannot throw a new check exception or a more general exception than the overridden method declaration. For example: A method of the parent class declares an exception IOException to be checked, but an Exception cannot be thrown when this method is overridden. Because Exception is the parent of the IOException, only subclass exceptions of IOException can be thrown.
In the object-oriented principle, rewriting means that any existing method can be overridden. Examples are as follows:

TestDog.java file code:

class Animal{
public void move(){ System.out.println("Animal can move"); } } class Dog extends Animal{ public void move(){ System.out.println("Dog can run and walk"); } } public class TestDog{ public static void main(String args[]){ Animal a = new Animal(); // Animal Object Animal b = new Dog(); // Dog Object a.move();// Execute the method of the Animal class b.move();//method of executing Dog class } }
The above example compiled and run results are as follows:
Animals can move dogs can run and walk
As you can see in the above example, although b is of type Animal, it runs the move method of the Dog class.
This is because during the compilation phase, only the reference type of the parameter is checked.
However, at runtime, the Java virtual machine (JVM) specifies the type of the object and runs the object's methods.
Therefore, in the above example, the reason why the compiler can succeed is because there is a move method in the Animal class, but at runtime, it is a method that runs a specific object.
Consider the following example:

TestDog.java file code:

class Animal{ public void move(){ System.out.println("Animal can move"); } } class Dog extends Animal{ public void move(){ System.out.println("Dog can run and walk"); } public void bark(){ System.out.println("The dog can buzz"); } } public class TestDog{ public static void main(String args[]){ Animal a = new Animal(); // Animal object Animal b = new Dog(); // Dog object a.move();//Execute the method of the Animal class b.move();//method of executing Dog class b.bark(); } }
The above example compiled and run results are as follows:
TestDog . java : 30 : cannot find symbol
Symbol   : Method Bark () 
LOCATION : class Animal 
                B . Bark (); ^  
                 
This program will throw a compilation error because b's reference type Animal does not have a bark method.

Rewrite rules for methods

  • The parameter list must be exactly the same as the overridden method;
  • The return type must be exactly the same as the return type of the overridden method;
  • Access rights cannot have lower access rights than methods that are overridden in the parent class. For example, if a method of a parent class is declared as public, overriding that method in a subclass cannot be declared protected.
  • Parent's member methods can only be overridden by its subclasses.
  • Methods declared as final cannot be rewritten.
  • Methods declared as static cannot be overridden but can be declared again.
  • Subclasses and parent classes are in the same package, so subclasses can override all methods of the parent class, except for methods declared as private and final.
  • Subclasses and parent classes are not in the same package, so subclasses can only override the non-final methods declared by the parent class as public and protected.
  • The overridden method can throw any non-forced exception regardless of whether the method being overridden throws an exception. However, the overriding method cannot throw a new mandatory exception, or a more extensive mandatory exception than the overridden method, and vice versa.
  • Constructor methods cannot be overridden.
  • If you cannot inherit a method, you cannot override this method.

Use of Super Keywords

When you need to call the superclass's overridden method in a subclass, use the super keyword.

TestDog.java file code:

class Animal{
public void move(){ System.out.println("Animal can move"); } } class Dog extends Animal{ public void move(){ super.move(); // method of applying super class System.out.println("Dog can run and walk"); } } public class TestDog{ public static void main(String args[]){ Animal b = new Dog(); // Dog object b.move(); //method of executing Dog class } }
The above example compiled and run results are as follows:
Animals can move dogs can run and walk

Overload

Overloading is in a class with the same method name and different parameters. The return type can be the same or different.
Each overloaded method (or constructor) must have a unique list of parameter types.
The most common place is the constructor overload.
Overload rules:
  • The overloaded method must change the parameter list (the number of parameters or type is not the same);
  • Overloaded methods can change the return type;
  • Overloaded methods can change access modifiers;
  • Overloaded methods can declare new or wider check exceptions;
  • Methods can be overloaded in the same class or in a subclass.
  • Cannot use the return value type as a criterion for overloading functions.

Examples

Overloading.java file code:

public class Overloading {
public int test(){ System.out.println("test1"); return 1; } public void test(int a){ System.out.println("test2"); } //The following two The parameter types are in different order public String test(int a,String s){ System.out.println("test3"); return "returntest3"; } public String test(String s,int a){ System.out.println("test4"); return "returntest4"; } public static void main(String[] args){ Overloading o = new Overloading(); System.out.println(o.test()); o.test(1); System.out.println(o.test(1,"test3")); System.out.println(o.test("test4",1)); } }

The difference between rewriting and reloading

Difference pointHeavy load methodRewrite method
parameter listMust be modifiedMust not be modified
Return typeCan be modifiedMust not be modified
abnormalCan be modifiedCan be reduced or deleted, must not throw new or wider exceptions
accessCan be modifiedMust not make stricter restrictions (can lower restrictions)

to sum up

Method Overriding and Overloading are different manifestations of java polymorphism. Rewriting is a manifestation of polymorphism between parent and child classes. Overloading can be interpreted as a polymorphic representation. form.

  • (1) Method overloading is defined in a class where more than one method name is the same, and their number of parameters is the same or different and the type and order are different. Overloading of methods is called.
  • (2) Method rewriting is a method in which a child class has the same name as a method of a parent class, and the number of arguments is the same as the type. A method that returns the same value is called Overriding.
  • (3) Method overloading is a polymorphic representation of a class, and method rewriting is a polymorphic representation of subclasses and parent classes.
  • Java Override vs Overload

Comments