Method overloading and overriding

Overloading
    1. Overloading of constructors, multiple constructors can be defined in a class, as long as the parameter types, number, and order of each constructor are different.

    2. Java's method overloading means that multiple methods can be created in a class, they have the same name, but have different parameters and different definitions.

When calling methods, it is determined which method to use by the different number of parameters and parameter types passed to them, which is polymorphism.


   3. When overloading, the method name must be the same, but the parameter type and number are different, and the return value type can be the same or different. The return type cannot be used to distinguish overloaded functions.
The different overloaded methods bark are distinguished according to their parameter types.

Constructor overloading
  
//Default no-argument constructor
	public Student(){
		
	}
	// overloading the constructor
        // Multiple constructors can be defined in a class, as long as the parameter types, number, and order of each constructor are different
		public Student(String s){
			
		}

		public Student(String s,String str){
			
		}
		
		public Student(String s,int t){
			
		}
		
		public Student(int t,String s){
			
		}


method overloading
     
  /**
	 * learning method
	 */
	public void study(){
		System.out.println(name+"learning");
	}
	
	/**
	 * Overload learning method
	 * You can define multiple methods with the same name in a class, as long as the number of parameters, type or order of the methods are different
	 * @param km subject to study
	 */
	public void study(String km){
		System.out.println(name+"learning"+km);
	}



Rewrite
    1. The polymorphism between the parent class and the child class redefines the function of the parent class. If a method is defined in a subclass with the same name and parameters as its parent class, we say the method is overridden. In Java, subclasses can inherit methods from the parent class without rewriting the same methods.

But sometimes the 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 known as method overriding.

    2. 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.

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326563868&siteId=291194637