honzeland

记录点滴。。。

常用链接

统计

Famous Websites

Java

Linux

P2P

最新评论

Override and Overload

Override: (覆盖)
1 、 Override 发生在继承中,使得一个子类 Override 其父类的一个 method 。
2
、 Override Rules : Keeping the contract 。子类 override 父类的 method 时,必须要保证 overriding method 和父类中的 overridden method 具有相同的对外协议 contract ,即相同的参数列表和兼容的返回类型。
3
、 Arguments must be the same, and return types must be compatible.
4
、 The method can't be less accessible. 考虑如下情况:如果访问权限变小了,如由父类 Parent 中的 public void test() 变成子类 Son 中的 private void test() ,如果编译器允许权限变小,在某一类中有如下语句: Parent p = new Son() ; p.test() ;则这些语句能够通过编译,当该类加载运行时,将会出现错误,因为父类 reference p 指向的是一子类 Son 的对象,而 Son 中的 test() 是 private ,不能在其他类中 invoke 。
      
另外,上面的语句在编译器允许权限变小情况下之所以能够通过编译,是因为在 “Parent p = new Son() ; ” 中,声明了一个父类 Parent 的 reference p ,由于 p 是父类的 reference ,由于父类中的 test() 是 public ,故 “p.test() ; ” 能够通过编译;而在运行时, p 指向的是一个子类的对象, p.test() 在运行时调用的是子类对象中的 test() 。

Overload
:(重载)
1 、重载:两个或多个 methods 具有相同的 name , 和 不同的 argument lists 。
2
、 The return types can be different.
3
、 You can't change ONLY the return type :不能仅仅改变返回类型, To overload a method, you MUST change the argument list, although you can change the return type to anything.
4
、 You can vary the access levels in any direction.

posted on 2006-11-10 10:37 honzeland 阅读(313) 评论(0)  编辑  收藏 所属分类: Java