Please go to DobbyKim’s Question of the Day for more questions

A:

First, overloading and overwriting have nothing to do with each other.

Overload means that we can define methods with the same name, differentiate them by defining different input parameters, and then when the method is called, the JVM will choose the appropriate method to execute based on the different parameter styles.

Sample program:

public class Overload {
    public void info(String name, int age) {
        System.out.println("my name is "
                + name
                + ",I'm " + age + " years old");
    }

    public void info(String name, int age, String hobby) {
        System.out.println("my name is "
                + name
                + ",I'm " + age + " years old,"
                + "and My hobby is " + hobby);

    }

    public static void main(String[] args) {
        Overload overload = new Overload();
        overload.info("Kim".27);
        overload.info("Kim".27."football"); }}Copy the code

Program output result:

my name is Kim,I'm 27 years old.
my name is Kim,I'm 27 years old,and My hobby is football

Copy the code

Override is the ability applied in polymorphism to make a subclass behave differently from its parent class. The new method overridden by the subclass overrides the old method of the parent class

Sample program:

public class Polymorphic {
    public static void main(String[] args) {
        // The parent class refers to the object of the subclass
        Animal cat = new Cat();
        cat.speak(); // Override the parent class's speak method}}class Animal {
    public void speak(a){
        System.out.println("I am an animal"); }}class Cat extends Animal{
    @Override
    public void speak(a) {
        System.out.println("I am a cat"); }}Copy the code

Program output result:

I'm a catCopy the code