Java rewrite and overloading methods and differences in detail

  • 2020-06-23 00:30:19
  • OfStack

Rewrite (Override)

Overrides are subclasses rewriting the implementation of the superclass's accessible methods! Neither the return value nor the formal parameters can be changed. That is, the shell remains unchanged and the core is rewritten!

The advantage of overrides is that subclasses can define their own behavior as needed.

This means that subclasses can implement the methods of the superclass as needed.

In object-oriented terms, override means that you can override any existing method. Examples are as follows:


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();//  perform  Animal  Methods of a class 
  b.move();// perform  Dog  Methods of a class 
 }
}

The compilation and operation results of the above instances are as follows:


 Animals can move 
 Dogs can run and walk 

As you can see in the above example, even though b is of type Animal, it runs the move method of the Dog class.

This is because at compile time, you just check the reference type of the parameter.

At run time, however, the Java virtual machine (JVM) specifies the type of object and runs the method of that object.

So in the above example, the compilation is successful because the move method exists in the Animal class, whereas at runtime, it runs the object-specific method.
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  object 
  Animal b = new Dog(); // Dog  object 
  a.move();//  perform  Animal  Methods of a class 
  b.move();// perform  Dog  Methods of a class 
  b.bark();
 }
}

The compilation and operation results of the above instances are as follows:


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 does not have bark methods.

Method rewrite rule

The argument list must be exactly the same as the overridden method;

The return type must be exactly the same as that of the overridden method;

The access rights cannot be higher than the overridden methods in the parent class. For example, if a method in a parent class is declared as public, overwriting that method in a child class cannot be declared as protected.

A member method of a superclass 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.

Subclasses and superclasses are in the same package, so a subclass can override all methods of the superclass except those declared as private and final.

The subclass and the parent are not in the same package, so the subclass can override only the non-ES79en methods declared by the parent as public and protected.

Overridden methods can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, an overridden method cannot throw a new mandatory exception, or a broader mandatory exception than the one declared by the overridden method, or vice versa.

Constructor cannot be overridden.

You cannot override a method if you cannot inherit it.

Use of the Super keyword

The super keyword is used when an overridden method of the parent class needs to be called in a subclass.


class Animal{
 public void move(){
  System.out.println(" Animals can move ");
 }
}
class Dog extends Animal{
 public void move(){
  super.move(); //  application super Methods of a 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(); // perform  Dog Methods of a class 
 }
}

The compilation and operation results of the above instances are as follows:


 Animals can move 
 Dogs can run and walk 

Overloading (Overload)

Overloading (overloading) is in a class with the same method name but different parameters. What about the return type? It can be the same or it can be different.

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

You can only overload constructors

Overloading rules

Overloaded methods must change the parameter list;

Overloaded methods can change the return type;

Overloaded methods can change the access modifier;

Overloaded methods can declare new or broader check exceptions;

Methods can be overloaded in the same class or in a subclass.

The instance


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));
 }
}

The difference between overwriting and overloading

区别点 重载方法 重写方法
参数列表 必须修改 1定不能修改
返回类型 可以修改 1定不能修改
异常 可以修改 可以减少或删除,1定不能抛出新的或者更广的异常
访问 可以修改 1定不能做更严格的限制(可以降低限制)

I hope this article will be helpful to you


Related articles: