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<Vehicle> vehicles = new ArrayList<Vehicle>();
      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";}
   }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值