YouTip LogoYouTip

Java Override Overload

# Java Override and Overload * * * ## Override Overriding 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 subclass's method implementation overrides the parent class's method implementation. **That is, the shell remains unchanged, but the core is overridden!** The benefit of overriding is that a subclass can define behavior specific to its own needs. In other words, a subclass can implement the parent class's method as needed. This way, when the method is called using a subclass object, the method in the subclass will be executed instead of the method in the parent class. An overriding method cannot throw new checked exceptions or exceptions that are broader than those declared by the overridden method. For example: if a method in the parent class declares a checked exception `IOException`, you cannot throw an `Exception` when overriding this method, because `Exception` is the parent class of `IOException`. You can only throw `IOException` or a subclass of `IOException`. In object-oriented principles, overriding means you can override any existing method. The example is as follows: ## TestDog.java File Code: ```java 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();// executes the method of Animal class b.move(); // executes the method of 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, you can see 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 method of that object. Therefore, in the above example, the reason it compiles successfully is that the `move` method exists in the `Animal` class, but at runtime, the method of the specific object is executed. Consider the following example: ## TestDog.java File Code: ```java 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();// executes the method of Animal class b.move(); // executes the method of 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(); ^ This program will throw a compilation error because the reference type of `b` is `Animal`, which 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 (in Java 5 and earlier, the return type must be the same; in Java 7 and later, it can be different). * 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 this method in the subclass cannot declare it as `protected`. * Member methods of a parent class 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 re-declared. * If the subclass and the parent class are in the same package, the subclass can override all methods of the parent class, except those declared as `private` and `final`. * If the subclass and the parent class are not in the same package, the subclass can only override the parent class's `public` and `protected` non-`final` methods. * Overriding methods can throw any non-checked (runtime) exceptions, regardless of whether the overridden method throws exceptions. However, overriding methods cannot throw new checked exceptions or checked exceptions that are broader than those declared by the overridden method, but the reverse is allowed. * Constructors cannot be overridden. * If you cannot inherit a class, you cannot override its methods. * * * ## Using the Super Keyword When you need to call the overridden method of the parent class in a subclass, you must use the `super` keyword. ## TestDog.java File Code: ```java class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ public void move(){ super.move(); // call the method of the super class 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(); // executes the method of Dog class } } The compilation and execution result of the above example is as follows: Animals can move Dogs can run and walk ## Overloading Overloading means having multiple methods with the same name but different parameter lists within the same class. 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:** * The overloaded method must change the parameter list (different number or type of parameters); * The overloaded method can change the return type; * The overloaded method can change the access modifier; * The overloaded method can declare new or broader checked exceptions; * Methods can be overloaded within the same class or in a subclass. * The return value type cannot be used as a criterion to distinguish overloaded functions. ### Example ## Overloading.java File Code: ```java 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 orders 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)); } } * * * ## Difference Between Overriding and Overloading | Difference Point | Overloaded Method | Overridden Method | | :--- | :--- | :--- | | Parameter List | Must be changed | Must not be changed | | Return Type | Can be changed | Must not be changed | | Exceptions | Can be changed | Can be reduced or removed, but must not throw new or broader exceptions | | Access | Can be changed | 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 a parent class and a subclass, while overloading can be understood as a specific form of polymorphism. * (1) Method overloading is when multiple methods with the same name are defined in a class, but their parameters differ in number, or have the same number but different types and orders. This is called method overloading. * (2) Method overriding is when a subclass has a method with the same name as a method in the parent class, and the number and type 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 a subclass and a parent class. !(#) !(#)
← Java PolymorphismJava Inheritance β†’