Java Override/Overload



Override

Override is the subclass's rewriting of the implementation process of the parent class's methods that allow access! 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 overriding is that subclasses can define their own behavior as needed.

That is to say, the subclass can implement the methods of the parent class as needed.

In object-oriented principles, overriding means that any existing method can be overridden. The example is as follows:

class Animal{

   public void move(){
      System.out.println("动物可以移动");
   }
}

class Dog extends Animal{

   public void move(){
      System.out.println("狗可以跑和走");
   }
}

public class TestDog{

   public static void main(String args[]){
      Animal a = new Animal(); // Animal 对象
      Animal b = new Dog(); // Dog 对象

      a.move();// 执行 Animal 类的方法

      b.move();//执行 Dog 类的方法
   }
}

The compilation and running results of the above example are as follows:

动物可以移动
狗可以跑和走

As you can see in the above example, although b belongs to the Animal type, 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 object and runs the object's methods.

So in the above example, the reason why the compilation is successful is because the move method exists in the Animal class. However, at runtime, the method of the specific object is run.

Think about the following example:

class Animal{

   public void move(){
      System.out.println("动物可以移动");
   }
}

class Dog extends Animal{

   public void move(){
      System.out.println("狗可以跑和走");
   }
   public void bark(){
      System.out.println("狗可以吠叫");
   }
}

public class TestDog{

   public static void main(String args[]){
      Animal a = new Animal(); // Animal 对象
      Animal b = new Dog(); // Dog 对象

      a.move();// 执行 Animal 类的方法
      b.move();//执行 Dog 类的方法
      b.bark();
   }
}

The compilation and running results of the above example 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 the reference type of b, Animal, does not have a bark method.


Method rewriting rules

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

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

  •             Access rights cannot be lower than the access rights of the overridden method in the parent class. For example: If a method of the parent class is declared as public, then the method cannot be declared as protected when overriding it in the subclass.

  •           Member methods of a parent class can only be overridden by its subclasses.

  •           Methods declared final cannot be overridden.

  •           Methods declared static cannot be overridden, but they 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 methods 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 of the parent class that are declared public and protected.

  •           An overridden method can throw any unforced exception, regardless of whether the overridden method throws an exception. However, an overridden method cannot throw new mandatory exceptions, or mandatory exceptions that are broader than those declared by the overridden method, and vice versa.

  •           Constructors cannot be overridden.

  • If you cannot inherit a method, you cannot override this method.


Use of Super keyword

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

class Animal{

   public void move(){
      System.out.println("动物可以移动");
   }
}

class Dog extends Animal{

   public void move(){
      super.move(); // 应用super类的方法
      System.out.println("狗可以跑和走");
   }
}

public class TestDog{

   public static void main(String args[]){

      Animal b = new Dog(); // Dog 对象
      b.move(); //执行 Dog类的方法

   }
}

The compilation and running results of the above example are as follows:

动物可以移动
狗可以跑和走

Overload(Overload)

Overloading (overloading) is in a class, the method name is 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 parameter type list.

Only constructors can be overloaded

Overloading rules

  • The overloaded method must change the parameter list;

  • The overloaded method can change the return type;

  • The overloaded method can change the access modifier;

  • Overloaded methods can declare new or wider checked exceptions;

  • methods can be overloaded in the same class or in a subclass.

Example

public class Overloading {
 
	public int test(){
		System.out.println("test1");
		return 1;
	}
 
	public void test(int a){
		System.out.println("test2");
	}	
 
	//以下两个参数类型顺序不同
	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

Difference pointsOverloaded methodOverridden method
Parameter listMust be modifiedMust not be modified
Return typeCan be modifiedMust not be modified
ExceptionCan be modified Can be reduced or deleted, must not throw new or wider exceptions
AccessCan be modifiedMust be Cannot make stricter restrictions (can lower restrictions)