Detailed explanation of Override and Overload


Override

The polymorphism between the parent class and the child class redefines the methods (functions) of the parent class .
If a method defined in a subclass has the same name and parameters as its parent class, we say that the method is overridden.
In Java, subclasses can inherit the methods in the parent class without the need to rewrite the same methods.
Sometimes a subclass does not want to inherit the method of the parent class intact, but wants to make certain modifications, which requires method rewriting.

Method overriding is also called method overriding . If a method in the subclass has the same method name, return type, and parameter list as a method in the parent class, the new method will overwrite the original method.

If you need the original method in the parent class, you can use the super keyword, which refers to the parent class of the current class.

Note: The access modification authority of the subclass function cannot be less than that of the parent class;

  • Rewriting is the process of rewriting the implementation process of the accessible method of the parent class by the subclass, and 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 specific behaviors as needed. In other words, the subclass can implement the method of the parent class as needed .
  • The overridden method cannot throw new checked exceptions or exceptions that are broader than the overridden method declaration . For example: A method of the parent class declares a check exception IOException, but you cannot throw Exception when overriding this method, because Exception is the parent of IOException and can only throw the subclass of IOException.

The code is as follows (example):

	class Person{
    
    
		public void introduce(){
    
    
			System.out.println("我是人类");
		}
	}
	 
	class Teacher extends Person{
    
    
		/**
		* 重写了父类 Person 的 introduce 方法
		*/
		public void introduce(){
    
    
			System.out.println("我是老师");
		}
	}
	 
	class Student extends Person{
    
    
		/**
		* 重写了父类 Person 的 introduce 方法
		*/
		public void introduce(){
    
    
			System.out.println("我是学生");
		}
	}
	 
	public class TestPerson {
    
    
		public static void main(String args[]){
    
    
			Person a = new Person(); 
			Person b = new Teacher (); 
			Person c = new Student (); 
			
			a.introduce();
			b.introduce();
			c.introduce();
		}
	}

Method rewriting rules

  1. The parameter list and the parameter list of the overridden method must be exactly the same.
  2. The return type and the return type of the overridden method can be different, but it must be a derived class of the return value of the parent class (the return type of java5 and earlier versions must be the same, and the return type of java7 and later versions can be different).
  3. The access authority cannot be lower than the access authority of the overridden method in the parent class. For example: if a method of the parent class is declared as public, then overriding the method in the child class cannot be declared as protected.
  4. The member methods of the parent class can only be overridden by its subclasses.
  5. A method declared as final cannot be overridden.
  6. A method declared as static cannot be overridden, but it can be declared again.
  7. If the subclass and the superclass are in the same package, the subclass can override all methods of the superclass, except for the methods declared as private and final.
  8. The subclass and the parent class are not in the same package, so the subclass can only override the non-final methods declared as public and protected of the parent class.
  9. The overridden method can throw any non-mandatory exception, regardless of whether the overridden method throws an exception. However, the overridden method cannot throw a new mandatory exception, or a broader mandatory exception than the one declared by the overridden method, and vice versa.
  10. The construction method cannot be overridden.
  11. If you cannot inherit a class, you cannot override the methods of that class.

Overload

Overload in a single class , method of the same name , but different parameters . The return type can be the same or different .
Each overloaded method (or constructor) must have a unique list of parameter types .
Tip: The most common place is the overloading of the constructor.

Method overloading is a manifestation of polymorphism in a class.
Method overloading is a means for classes to handle different types of data in a uniform way.
Multiple functions with the same name exist at the same time, with different numbers or types of parameters.
Method overloading in Java means that multiple methods can be created in a class. They have the same name but different parameters and different definitions.
When calling methods, the number of different parameters passed to them and the types of parameters determine which method to use. This is polymorphism.

Note: The return type cannot be used as the criterion for distinguishing overloaded functions.


The code is as follows (example):

	public class Overload {
    
    
		public int test(){
    
    
			System.out.println("test1");
			return 1;
		}
	
		/**
		* 以下三个重载了本类的 test 方法
		*/
		public void test(int a){
    
    
			System.out.println("test2");
		}   
		
		/**
		* 以下两个参数类型顺序不同
		*/
		public String test(int a,String s){
    
    
			System.out.println("test3");
			return "return test3";
		}   
		
		public String test(String s,int a){
    
    
			System.out.println("test4");
			return "return test4";
		}   
		
		public static void main(String[] args){
    
    
			Overload overload  = new Overload();
			System.out.println(o.test());
			o.test(1);
			System.out.println(o.test(1,"test3"));
			System.out.println(o.test("test4",1));
		}
	}

Method overloading rules:

  1. The overloaded method must change the parameter list (the number or types of parameters are not the same).
  2. The overloaded method can change the return type.
  3. The overloaded method can change the access modifier.
  4. Overloaded methods can declare new or broader checked exceptions.
  5. Methods can be overloaded in the same class or in a subclass.
  6. The return value type cannot be used as the criterion for distinguishing overloaded functions.

The difference between rewriting and reloading

Overload:

  1. Occurs in the same class;
  2. The same method name;
  3. The parameter list is different;
  4. Regardless of the return value, if there is an "overload" with a different return value, it is wrong;

Rewrite:

  1. Occurs in subclasses and parent classes;
  2. The same method name;
  3. The same parameter list;
  4. The return value is the same or the return value of the subclass method is a subclass of the return value type of the parent method;
  5. The access modifier is the same or the modifier range of the subclass method is larger than the parent class;
  6. The exceptions thrown are the same or the exceptions thrown by the subclass method are smaller than the parent class;

Method overriding (Overriding) and overloading (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 the specific manifestation of polymorphism. form.

  1. Method overloading is defined in a class with multiple methods with the same name, but their number of parameters is different or the number is the same but the type and order are different, it is called method overloading.
  2. Method overriding is a method in which the name of the method in the subclass is the same as the method of the parent class, and the number of parameters is the same as the type, and the return value is the same method, which is called overriding.
  3. Method overloading is a polymorphism of a class, and method overriding is a polymorphism of subclasses and parent classes.

Comparative analysis of differences:

Difference Overloaded method Rewrite method
parameter list Must be modified Must not be modified
Return type Can be modified Must not be modified
abnormal Can be modified Can be reduced or deleted, and must not throw new or broader exceptions
access Can be modified Must not make stricter restrictions (restrictions can be lowered)

Guess you like

Origin blog.csdn.net/baidu_41847368/article/details/114671713