Java中的overload和override

今天去笔试,有一个问题是请写出overload和override的区别,

我愣了半天,最终还是没有写出来,没写出来的理由也是一大堆,

所以今天重温一下.

Overriding a method means that its entire functionality is being replaced.
Overriding is something done in a child class to a method defined in a parent
class.
The methods with the same name and same number of arguments and types but one
is in base class and second is in derived class.
override对应于OO编程的继承特性,意思就是在子类中重写父类中的方法;
满足方法重写的几个条件:
1.方法名相同.
2.子类的访问控制权限不能低于父类被重写方法的访问权限.
3.参数类型,参数个数,参数列表顺序,必须与父类被重写方法的一致.
4.返回值类型相同.

Overloading of methods is a compiler trick to allow you to use the same name
to perform different actions depending on parameters.
The methods with the same name but it differs by types of arguments and number
of arguments.
overload对应于OO编程的多态特性,意思就是在同一个类中同样名称的多个方法, 这多个方法之间的区别在他们的参数列不同。
1.方法名相同
2.参数类型,参数个数,参数列表顺序不相同

package interface1;

import java.util.ArrayList;
import java.util.List;

/**

  • Class demonstrating overloading/overriding issues in Java.
    */
    public class OverloadingExample
    {
    /**
  • Print out a Vehicle’s toString().
  • @param theVehicle The Vehicle whose toString() will be executed.
    */
    public void print(final Vehicle theVehicle)
    {
    System.out.println(“Vehicle: “ + theVehicle);
    }

/**

  • Print out an Automobile’s toString().
  • @param theAutomobile The Automobile whose toString() will be executed.
    */
    public void print(final Automobile theAutomobile)
    {
    System.out.println(“Automobile: “ + theAutomobile);
    }

/**

  • Print out a Car’s toString().
  • @param theCar The Car whose toString() will be executed.
    */
    public void print(final Car theCar)
    {
    System.out.println(“Car: “ + theCar);
    }

/**

  • Main test to be executed.
    */
    public void runTest()
    {
    System.out.println(
    “===== COMPILE-TIME RESOLUTION IS SUFFICIENT FOR OVERLOADING =====”);
    print(new Vehicle());
    print(new Car());
    print(new Automobile());
    System.out.println(
    “===== COMPILE_TIME RESOLUTION NOT SUFFICIENT FOR OVERRIDE=====”);
    final List vehicles = new ArrayList();
    vehicles.add(new Car());
    vehicles.add(new Automobile());
    vehicles.add(new Vehicle());
    for (final Vehicle vehicle : vehicles)
    {
    print(vehicle);
    }
    }

/**

  • Main function to run the test demonstrating Java’s overloading and
  • overriding.
    */
    public static void main(final String[] arguments)
    {
    final OverloadingExample me = new OverloadingExample();
    me.runTest();
    }

/**

  • Parent class with its own toString implementation.
    */
    public static class Vehicle
    {
    @Override
    public String toString() {return “Vehicle”;}
    }

/**

  • Child class with its own toString implementation.
    */
    public static class Automobile extends Vehicle
    {
    @Override
    public String toString() {return “Automobile”;}
    }

/**

  • Grandchild class with its own toString implementation.
    */
    public static class Car extends Automobile
    {
    @Override
    public String toString() {return “Car”;}
    }
    }