Is it overloading or overriding?

2019-07-11 14:48发布

问题:

I have a code and I am not able to get why the output will be "Radial Tire with long".Can somebody help me to understand this code?

class Tyre {
    public void front() throws RuntimeException {
        System.out.println("Tire");
    }

    public void front(long a) {
        System.out.println("Radial Tire with long");
    }
}

class TestSolution extends Tyre {
    public void front() {
        System.out.println("Radial Tire");
    }
    public void front(int a) throws RuntimeException {
        System.out.println("Radial Tire with int");
    }


public static void main(String... args) {
        Tyre t = new TestSolution();
        int a = 10;
        t.front(a);

}

}

回答1:

front is not overridden in TestSolution, it is overloaded.

You can regard an overloaded function as a completely different function, like one with a different name.

So t.front(a) will call the one in Tyre, with an a implicitly converted to long.



回答2:

So if we go with definitions

Overloading means methods with same name but with different number or order of parameters.

Overriding means method with same name with same number of parameters along with rules mentioned here

So in your case front method is overloaded in both the classes Tyre and TestSolution

method front() from Tyre class is overridden in class TestSolution.

no overriding in case of method front(long a) and front(int a).



回答3:

There's no overriding taking place in your main.

t's static (compile-time) type is Tyre, so, since method overload resolution is determined by the compile-time type of the instance, the only front methods available for the compiler to choose from are those declared in the base class Tyre :

public void front()
public void front(long a)

Only the latter (public void front(long a)) matches the arguments of the call t.front(a), and that method is not overridden by the sub-class. Therefore Radial Tire with long is displayed.

Calling ((TestSolution)t).front(a); would invoke the sub-class's method - public void front(int a).



回答4:

General rule: if I have a variable of one class I can access only methods and components defined in that class.

The only particular case is:

  1. you have a component or method both in the super class and in the subclass (overriding)
  2. you have a variable of the super class and an object of the subclass (your case)

In these cases you can follow the following rule:

  1. for the components the type of the variable decides which to use
  2. for the methods the type of the object does (late binding)

In your case as stated before the method is not overridden so you can't apply the last rule.



回答5:

Lets understand the difference between overloading and overriding

Overloading:

The Java programming language supports overloading methods, and Java can distinguish between methods with different method signatures. This means that methods within a class can have the same name if they have different parameter lists

Overriding:

An instance method in a subclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclass overrides the superclass's method.

Tyre declared front() method with long as parameter.

TyreSolution declared front() method with int as parameter.

Since the method signature is different, TyreSolution overloads Tyre's front() method

If you change signature of front() in TyreSolution to accept long value as input parameter instead of int, then TyreSolution overrides Tyre's front() method.

e.g. TestSolution class definition of front() method

public void front(long a) throws RuntimeException {
    System.out.println("Radial Tire with long in TestSolution");
}

output:

Radial Tire with long in TestSolution