`
4ever000_1988
  • 浏览: 24412 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

抽象类与接口的区别

    博客分类:
  • java
阅读更多

    抽象类与接口的区别(英文描述):

    Interface is open to the public, there can not have the private methods and variables.It is used to let others use, but the abstract class can have private  methods or private variables.In addition, the realization of the interface must be realized in the interface definition of all methods, and the realization  of the abstract category have the choice of rewriting need to use the method, the general application, is the top of the interface, and then to achieve an  abstract category Interface, and finally to achieve specific category. Furthermore, the interface can be achieved multiple inheritance, a class can only  inherit a super-category, but can be inherited through multiple interface multiple inheritance, the interface is also available logo (There are no any way,  such as the Remote Interface) and data Sharing (there are all the variables constant) role.

    中文翻译:接口是公开的,里面不能有私有的方法或变量,是用于让别人使用的,而抽象类是可以有私有方法或私有变量的,另外,实现接口的一定要实现接口里定义的所有方法,而实现抽象类可以有选择地重写需要用到的方法,一般的应用里,最顶级的是接口,然后是抽象类实现接口,最后才到具体 类实现。还有,接口可以实现多重继承,而一个类只能继承一个超类,但可以通过继承多个接口实现多重继承,接口还有标识(里面没有任何方法,如Remote接口)和数据共享(里面的变 量全是常量)的作用。

    抽象类和接口的区别在于使用动机。使用抽象类是为了代码的复用,而使用接口的动机是为了实现多态性。

    抽象类和它的子类之间应该是一般和特殊的关系,是一种"is-a"的关系,子类继承父类所有非private的数据成员和成员方法,而接口仅仅是它的子类应该实现的一组规则。如,交通工具定义成抽象类,汽车、飞机、轮船定义成子类,是可以接受的,因为汽车、飞机、轮船都是一种特殊的交通工具。再譬如Icomparable接口,它只是说,实现这个接口的类必须要可以进行比较,这是一条规则。如果Car这个类实现了Icomparable,只是说,我们的Car中有一个方法可以对两个Car的实例进行比较,可能是比哪辆车更贵,也可能比哪辆车更大。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics