What exactly is the difference between Java method overloading and method overriding?

  • 2021-09-16 06:56:59
  • OfStack

The difference between method overloading and method overriding

Method overload

Method overloading is mainly in one class, the method name of the method is the same, the parameter list is different, and the return value type can be the same or different.


/*
 This is just a simple example 1 Under ,Food Snack Not given , Meaning 1 Just go down .
*/
public class Demo{
	public void eat(Food food){
		System.out.println(" Eat normally today ! Eat " + food.name);
	}
	
	public void eat(Snack snack){
		System.out.println(" I want to eat snacks today ! Eat " + snack.name);
	}
	
	public int eat(){
		int money = 10;
		System.out.println(" Drink northwest wind today ! But it saves money , Returns the amount saved ");
		return 10;
	}
	
	//public void eat(){
	//	int money = 10;
	//	System.out.println(" Drink northwest wind today ! But it saves money , Returns the amount saved ");
	//}
}

Note: If the parameter list is the same, the return value type is not overloaded. This writing will report an error directly. Because we don't know the return value type of the method when calling the method, the compiler can't distinguish which method you are calling, so it must be silly, so I will tell you, brother, you made an error!

Method override

Method overrides are embodied in the child parent class, and the most typical example is that the child class overrides the method in the parent class.

Both the Father and Son classes have Walk (boolean canRun) methods.
But when executed, the results are different.


public class Father {

    private boolean canRun = false;

    public boolean isCanRun() {
        return canRun;
    }

    public void setCanRun(boolean canRun) {
        this.canRun = canRun;
    }

    public void walk(boolean canRun){
        if(canRun){
            System.out.println(" Father ran and ran as hard as he could !");
        }else{
            System.out.println(" Father is old , You can only walk !");
        }
    }
}

public class Son extends Father{

    boolean canRun;

    public Son(boolean canRun){
        this.canRun = canRun;
    }

    @Override
    public void walk(boolean canRun) {
        if(canRun){
            System.out.println(" Son can run !");
        }else{
            System.out.println(" My son can only go !");
        }
    }
}

public class Test {
    public static void main(String[] args) {
        Father father = new Father();
        Son son = new Son(true);
        father.walk(father.isCanRun());
        son.walk(son.canRun);
    }
}

This shows that the subclass Son overrides the Walk () method in the parent class.
Note: An important point about overrides here is that the method overridden by a subclass cannot have lower access rights than the method of the parent class.
You can simply understand 1: In polymorphism, the parent class reference points to the subclass object. If the overridden method permission in the subclass object is lower than that of the parent class method, then the parent class reference cannot access the method in the subclass in polymorphism.


public class Test {
    public static void main(String[] args) {
        Father father = new Son(true);
        System.out.println(father.isCanRun());	// false
        father.walk(father.isCanRun());			//  My son can only go !
    }
}

It's amazing here. When my Son object was created, the parameter passed in was true. Why did the execution result be true 儿子也只能走! What about?

Here is a summary of this knowledge point, and the above problems will be explained in the next article.

Summary

1. The method overload is now the same as a class with the same name method whose parameter list is different (must be satisfied), and the return value type can be the same or different. If two methods have the same method name and parameter list but the return value type is different, it is not overloaded.

2. Method override is embodied in the child parent class and the connection between different classes. The method required to be overridden by the child class has the same method name, parameter list and return value type as the parent class, but the specific operation within the method can be changed. When the method is overridden, it is required that the template permission of the child class method should not be lower than the access permission of the parent class method


Related articles: