Java rewriting and overloading

Override

   rewriting is a subclass's rewriting of the implementation process of the accessible methods of the parent class, and the return value and formal parameters cannot be changed (that is, the shell remains unchanged, and the core is rewritten).
   the advantage of rewriting is that subclasses can define their own specific behaviors as needed, that is, subclasses can implement the methods of their parent classes as needed.
  overriding a method cannot throw a new check Exception or an Exception broader than the declaration of the overridden method. For example, a method of the parent class declares a check Exception IOException, but the subclass cannot throw an Exception when overriding this method, because Exception is the parent class of IOException and can only throw subclass exceptions of IOException.
  in the object-oriented principle, rewriting means that any existing method can be rewritten:

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 b = new Dog();
        a.move();
        b.move();
    }
}

Execution results:

Animals can move
 Dogs can run and walk

In the above example, we can see that although b is of type Animal, it runs the move method of Dog class. This is because in the compilation phase, only the reference type of the parameter is checked; However, at run time, the Java virtual machine specifies the type of object and runs the method of the object. Therefore, the reason why the above example can be compiled successfully is that there is a move method in the Animal class, but at run time, it runs the method of a specific object.
  consider the following examples:

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 b = new Dog();
        a.move();
        b.move();
        b.bark();
    }
}

Execution results:

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

The program will throw a compilation error because b's reference type Animal has no bark method.

Override rules for methods

  • The parameter list must be exactly the same as that of the overridden method, and the return type must be exactly the same as that of the overridden method.
  • The access permission cannot be lower than the access permission of the overridden method in the parent class. For example, if a method of a parent class is declared public, overriding the method in a child class cannot be declared 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 can be declared again.
  • 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 non final methods declared as public and protected by the parent class.
  • The overridden method can throw any non mandatory exception, whether or not the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception or a broader mandatory exception than that declared by the overridden method, and vice versa.
  • Constructor cannot be overridden.
  • If you cannot inherit a method, you cannot override it.

Use of super keyword

When you need to call a rewrite method of a parent class in a subclass, use the super keyword:

class Animal {
    public void move() {
        System.out.println("Animals can move");
    }
}

class Dog extends Animal {
    public void move() {
        super.move(); /* Call the method of super class */
        System.out.println("Dogs can run and walk");
    }
}

public class TestDog {
    public static void main(String args[]) {
        Animal b = new Dog();
        b.move();
    }
}

Execution results:

Animals can move
 Dogs can run and walk

Overload

   overloading is in a class. The method names are the same, but the parameters are different. The return types 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 overloading of constructors.
  overload rules are as follows:

  • The overloaded method must change the parameter list (the number or type of parameters are different).
  • Overloaded methods can change the return type.
  • Overloaded methods can change access modifiers.
  • Overloaded methods can declare new or broader check exceptions.
  • Methods can be overloaded in the same class or in a subclass.
  • The return value type cannot be used as a distinguishing criterion for overloaded functions.
public class Overloading {
    public int test() {
        System.out.println("test1");
        return 1;
    }

    public void test(int a) {
        System.out.println("test2");
    }

    /* The following two 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));
    }
}

Execution results:

test1
1
test2
test3
returntest3
test4
returntest4

  the difference between rewriting and overloading:

Distinguishing pointsOverloading methods override method
parameter listMust be modifiedIt must not be modified
Return typeCan be modifiedIt must not be modified
abnormalCan be modifiedIt can be reduced or deleted, and new or broader exceptions must not be thrown
visitCan be modifiedThere must be no stricter restrictions (restrictions can be reduced)

summary

  method rewriting and overloading are different manifestations of java polymorphism. Rewriting is a manifestation of polymorphism between parent and child classes. Overloading can be understood as a specific manifestation of polymorphism.

  • Method overloading means that multiple methods with the same name are defined in a class, but the number of their parameters is different, or the number is the same but the type and order are different.
  • Method rewriting is a method that exists in a subclass, has the same name as the method of the parent class, has the same number and type of parameters, and has the same return value.

Keywords: Java

Added by Exdaix on Sun, 16 Jan 2022 23:58:30 +0200