Java Override and Overload


Override

Override means that a subclass defines a method with the same name, parameter list, and return type as a method in its parent class, and the implementation of the subclass method overrides the implementation of the parent class method.That is, the shell remains unchanged, but the core is rewritten!

The benefit of overriding is that a subclass can define behavior specific to itself as needed. That is, the subclass can implement the parent class's method as needed. In this way, when a subclass object is used to call the method, the method in the subclass is executed rather than the method in the parent class.

An overriding method cannot throw new checked exceptions or exceptions broader than those declared by the overridden method. For example: a method in the parent class declares a checked exception IOException, but when overriding this method, you cannot throw Exception, because Exception is the parent class of IOException. You may throw IOException or a subclass exception of IOException.

In object-oriented principles, overriding means that any existing method can be overridden. Here is an example:

TestDog.java file code:

class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ public void move(){ System.out.println("Dogs 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();//Execute the method of the Dog class } }

The compilation and execution result of the above example is as follows:

Animals can move
Dogs can run and walk

In the above example, it can be seen that 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) determines the type of the object and runs the object's method.

Therefore, in the above example, compilation succeeds because the Animal class has the move method; however, at runtime, the method of the specific object is executed.

Consider the following example:

TestDog.java file code:

class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ public void move(){ System.out.println("Dogs can run and walk"); } public void bark(){ System.out.println("Dogs can bark"); } } 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();//Execute the method of the Dog class b.bark(); } }

The compilation and execution result of the above example is as follows:

TestDog.java:30: cannot find symbol
symbol  : method bark()
location: class Animal
                b.bark();
                 ^

The program will throw a compilation error because the reference type of b, Animal, does not have a bark method.


Method Overriding Rules

  • The parameter list must be exactly the same as the parameter list of the overridden method.

  • The return type can be different from the return type of the overridden method, but it must be a derived class of the parent class's return value (the return type must be the same in Java 5 and earlier versions, and can be different in Java 7 and later versions).

  • The access level cannot be lower than the access level of the overridden method in the parent class. For example: if a method in the parent class is declared as public, then overriding that method in the subclass cannot declare it as protected.

  • A parent class's member methods can only be overridden by its subclasses.

  • Methods declared as final cannot be overridden.

  • Methods declared as static cannot be overridden, but they can be redeclared.

  • If the subclass and parent class are in the same package, the subclass can override all methods of the parent class, except methods declared as private and final.

  • If the subclass and parent class are not in the same package, the subclass can only override the parent class's non-final methods declared as public and protected.

  • An overriding method can throw any unchecked exception, regardless of whether the overridden method throws an exception. However, an overriding method cannot throw new checked exceptions, or broader checked exceptions than those declared by the overridden method, whereas the reverse is allowed.

  • Constructors cannot be overridden.

  • If a class cannot be inherited, its methods cannot be overridden.


Usage of the super Keyword

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

TestDog.java file code:

class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ public void move(){ super.move(); //Calling the parent class method using super System.out.println("Dogs can run and walk"); } } public class TestDog{ public static void main(String args[]){ Animal b = new Dog(); //Dog object b.move(); //Execute the method of the Dog class } }

The compilation and execution result of the above example is as follows:

Animals can move
Dogs can run and walk

Overload

Overloading means that within a class, methods have the same name but different parameters. The return type can be the same or different.

Each overloaded method (or constructor) must have a unique parameter type list.

The most common use is constructor overloading.

Overloading rules:

  • Overloaded methods must change the parameter list (the number or types of parameters must be different);
  • Overloaded methods can change the return type;
  • Overloaded methods can change the access modifier;
  • Overloaded methods can declare new or broader checked exceptions;
  • Methods can be overloaded in the same class or in a subclass.
  • The return type cannot be used as a criterion for distinguishing overloaded functions.

Example

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 have different parameter type 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)); } }

Differences Between Overriding and Overloading

Difference point Overloaded method Overridden method
Parameter list Must modify Must not modify
Return type Can modify Must not modify
Exception Can modify Can reduce or delete, must not throw new or broader exceptions
Access Can modify Must not impose stricter restrictions (can relax restrictions)

Summary

Method overriding and overloading are different manifestations of Java polymorphism. Overriding is a manifestation of polymorphism between the parent class and the subclass, and overloading can be understood as a specific form of polymorphism.

  • (1) Method overloading is when a class defines multiple methods with the same name, but their parameters differ in number, or have the same number but different types and order; this is called method overloading.
  • (2) Method overriding is when a method exists in a subclass with the same name as a method in the parent class, and the number and types of parameters are the same, and the return value is also the same; this is called overriding.
  • (3) Method overloading is a manifestation of polymorphism within a class, while method overriding is a manifestation of polymorphism between the subclass and the parent class.

Other Extensions