Method rewriting and overloading

Method override (override)

Requirements: the names of methods are consistent
Method rewriting ( cover ) It must happen between parent and child classes
public class T05 {
	public static void main(String[] args) {
		F4 f = new F4();
		f.pp(10);
		S4 s = new S4();
		s.pp(10);
		F4 fs = new S4();
		fs.pp(10);
	}
}

class F4 {
	public void pp(int k) {
		System.out.println("F4.pp(int)");
	}
}

class S4 extends F4 {
	public void pp(int k) {//The method with the same name and parameter defined in the subclass overrides the method definition in the parent class. If you need to call the method in the parent class, you need to use super.pp(k) to call it
		System.out.println("S4.pp(int)");
	}
}
  • Execution rule: new who runs whose method, regardless of the declared type
  • The coverage definition of the method requires the method name to be consistent

@The Override annotation enables the IDE tool to check when compiling the source code. If there is a handwriting error, the IDE tool will report an error

  • The parameters of the method are consistent (number, type, order), regardless of the parameter name
	Problems of the same type
	class Fa {
		public void eat(Integer kk) {
			System.out.println("Animal.eat()");
		}
	}

	class Son extends Fa {
		@Override
		public void eat(Number kk) {//The types must be consistent, even if the parent type is not allowed, int and Integer simple types and wrapper types are not allowed. If you remove the @override annotation here, there will be no syntax errors. This is not a method rewrite, but a method overload
			System.out.println("Just love mice");
		}
	}
Problems of consistent order , The system identification method depends on the method name and parameter type list, which is independent of the name of the method parameter. For example, the system identification method here is ear(String,String) . To recognize the order, we have to rely on the difference of types, such as eat(int,double) and
eat(double,int)
	class Fa {
		public void eat(String s1, String s2) {
			System.out.println("Animal.eat()");
		}
	}

	class Son extends Fa {
		@Override
		public void eat(String s2, String s1) {
			System.out.println("Just love mice");
		}
	}

 

The return data types are consistent [interview] (because if the return types are inconsistent, syntax check cannot be performed. For example, the parent class returns Double, while the subclass returns Integer. Is the syntax check at the call checked by Double or Integer? It is allowed that the parent type is returned in the parent class, and the subtype is returned in the subclass, for example, the Number type is returned in the parent class, and the Integer is returned in the subclass)

	class Fa {
		public Number eat(double s1, int s2) {
			System.out.println("Animal.eat()");
			return 10.;
		}
	}

	class Son extends Fa {
		@Override
		public Integer eat(double s2, int s1) {//Allow the return value type in the subclass to be the subtype of the return value type of the parent class
			System.out.println("Just love mice");
			return 99;
		}
	}
  • Throw exceptions consistently. Note that subclasses are actually allowed to throw fewer exceptions than their parent classes
	class Fa {
		public Integer eat(double s1, int s2) throws Exception {
			System.out.println("Animal.eat()");
			return 10;
		}
	}

	class Son extends Fa {
		@Override
		public Integer eat(double s2, int s1) throws IOException {
			System.out.println("Just love mice");
			return 99;
		}
	}
Require method scope in subclass >= Method scope in parent class
  • Static method overrides and calls, and whoever declares it will call whose static method
public class T05 {
	public static void main(String[] args) {
		// There is no problem in directly using specific classes to call static methods. The static methods defined in which classes are used are called
		// Fa.pp();Fa...pp
		// Son.pp();Son...pp
		// In fact, static methods can also be called by using objects after they are created. There is no problem that the type of the declared variable is consistent with the type of the specific build
		// Fa ff=new Fa();
		// ff.pp();
		// If a static method is called, the method of whom is called is declared by who
		Fa ff = new Son();
		ff.pp();// The execution result is Fa...pp
	}
}

class Fa {
	public static void pp() {
		System.out.println("Fa...pp");
	}
}

class Son extends Fa {
	public static void pp() {
		System.out.println("Son...pp");
	}
}

Overloading of methods

Methods have the same name and different parameters, regardless of the return value type. It can be within a class or between parent and child classes
Call rule: type best match principle
class A5 {
	public void pp() {
		System.out.println("A5.pp()");
	}

	public int pp() {// Syntax error, because the system recognizes a method with [method name + parameter type list], and the system will recognize these two pp methods as the same method. Note: the same method is not allowed in a class
	}

	public void pp(int k) {
		System.out.println("A5.pp(int)");
	}
}

class B5 extends A5 {
	public void pp(String k) {
		System.out.println("B5.pp(String)");
	}

	public void pp(String k, int k1) {
	}

	public void pp(int k, String k1) {
	}
}
Requirements: the method name is the same and the parameters are different. Different parameters are 3 Cases: different number of parameters, different types of parameters, and parameter order
Different order
  • Independent of parameter name
Independent of return type
  • Why do different return types judge different methods?
For example, the return value does not need to be processed when calling a method pp(10), Easy to cause confusion
public double pp(int k){} 
public int pp(int k){} 
A method has a return value, and the calling place can actually not receive it. Assuming that the system can recognize these two methods as different methods according to the return type, call pp(10)There is no way to judge which one to implement

Independent of scope

public void pp(){} 
protected void pp(){}Syntax error
It has nothing to do with the exception thrown by the method
Method overloading can occur between parent and child classes, or within a class. However, the coverage of methods must be between parent and child classes, and cannot
Implement method coverage within a class

Tags: Java

Posted by kdidymus on Tue, 09 Aug 2022 22:01:12 +0530