When should you use an abstract class, when an interface , when both? Interfaces and abstract classes seem superficially to provide almost the same capability. How do you decide which to use?

When To Use Interfaces

An interface allows somebody to start from scratch to implement your interface or implement your interface in some other code whose original or primary purpose was quite different from your interface . To them, your interface is only incidental, something that have to add on to the their code to be able to use your package.

When To Use Abstract classes

An abstract class, in contrast, provides more structure. It usually defines some default implementations and provides some tools useful for a full implementation. The catch is, code using it must use your class as the base. That may be highly inconvenient if the other programmers wanting to use your package have already developed their own class hierarchy independently. In Java, a class can inherit from only one base class.

When to Use Both

You can offer the best of both worlds, an interface and an abstract class. Implementors can ignore your abstract class if they choose. The only drawback of doing that is calling methods via their interface name is slightly slower than calling them via their abstract class name.

Summary Table

Interfaces vs Abstract Classes
feature interface abstract class

multiple inheritance

继承的多样性

A class may implement several interfaces.类可以实现多个接口

A class may extend only one abstract class.

类只能继承自一个抽象类

default implementation

默认实现

An interface cannot provide any code at all, much less default code.

接口不能提供任何实现,是最少的默认代码

An abstract class can provide complete code, default code, and/or just stubs that have to be overridden.

抽象类可以提供完整的代码,默认代码或者只留下那些需要被重载的方法

constants

常量

Static final constants only, can use them without qualification in classes that implement the interface . On the other paw, these unqualified names pollute the namespace. You can use them and it is not obvious where they are coming from since the qualification is optional. Both instance and static constants are possible. Both static and instance intialiser code are also possible to compute the constants.

third party convenience

第三方便利性

An interface implementation may be added to any existing third party class.

接口实现可以添加给任何已存的第三方类

A third party class must be rewritten to extend only from the abstract class.第三方类必须重写以便扩展自抽象类
is-a vs -able or can-do Interfaces are often used to describe the peripheral abilities of a class, not its central identity, e.g. an Automobile class might implement the Recyclable interface , which could apply to many otherwise totally unrelated objects. An abstract class defines the core identity of its descendants. If you defined a Dog abstract class then Dalmatian descendants are Dogs, they are not merely dogable. Implemented interfaces enumerate the general things a class can do, not the things a class is.

In a Java context, users should typically implement the Runnable interface rather than extending Thread , because they’re not really interested in providing some new Thread functionality, they normally just want some code to have the capability of running independently. They want to create something that can be run in a thread, not a new kind of thread.The similar is-a vs has-a debate comes up when you decide to inherit or delegate.

multiple inheritance for further discussion of is-a vs has-a
plug-in You can write a new replacement module for an interface that contains not one stick of code in common with the existing implementations. When you implement the interface, you start from scratch without any default implementation. You have to obtain your tools from other classes; nothing comes with the interface other than a few constants. This gives you freedom to implement a radically different internal design. You must use the abstract class as-is for the code base, with all its attendant baggage, good or bad. The abstract class author has imposed structure on you. Depending on the cleverness of the author of the abstract class, this may be good or bad.

homogeneity

相同性

If all the various implementations share is the method signatures, then an interface works best. If the various implementations are all of a kind and share a common status and behaviour, usually an abstract class works best. Another issue that’s important is what I call "heterogeneous vs. homogeneous." If implementors/subclasses are homogeneous, tend towards an abstract base class. If they are heterogeneous, use an interface . (Now all I have to do is come up with a good definition of hetero/homo-geneous in this context.) If the various objects are all of-a-kind, and share a common state and behavior, then tend towards a common base class. If all they share is a set of method signatures, then tend towards an interface .

maintenance

维护成本

If your client code talks only in terms of an interface , you can easily change the concrete implementation behind it, using a factory method . Just like an interface , if your client code talks only in terms of an abstract class, you can easily change the concrete implementation behind it, using a factory method .

speed

速度

Slow, requires extra indirection to find the corresponding method in the actual class. Modern JVMs are discovering ways to reduce this speed penalty.

Fast

terseness

精炼性

The constant declarations in an interface are all presumed public static final , so you may leave that part out. You can’t call any methods to compute the initial values of your constants. You need not declare individual methods of an interface abstract . They are all presumed so.

接口中常量声明都是预定的static的常量,所以你可以空着这部分。你不能调用任何方法去计算你的常量的初始值,你也不必声明单个方法是抽象的,他们默认就是这样的

You can put shared code into an abstract class, where you cannot into an interface . If interfaces want to share code, you will have to write other bubblegum to arrange that. You may use methods to compute the initial values of your constants and variables, both instance and static. You must declare all the individual methods of an abstract class abstract .

你可以把共享代码放到抽象类中,而你不能把这些放到接口中。假如接口想有一些共享代码,你将写其他东西组织他们。你可以使用方法计算你的常量和变量的初始值,不管是实例的还是静态的,你必须将抽象类的单个方法声明为抽象的,他们才是abstract方法

adding functionality

添加功能性

If you add a new method to an interface , you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method.

假如你给解开新添加一个方法,你必须在整个空间找出所有实现该接口的类,然后提供新添加的方法的具体实现。

If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.

假如你给抽象类新添加了新方法,可以提供一个默认的实现给这个新方法,那么所有已存的代码无需改动就可以继续工作

抽象类和接口的区别(之二)相关推荐

  1. 谈抽象类与接口的区别之一

    源:http://wenku.baidu.com/link?url=TYIBaNR8SjtDIDFO_E9ROFLlghKhAoW48qZXj_berCee07s9e9nXv5XNRYb10GZW_m ...

  2. 问题小结(二)——maven的核心功能、面向对象编程和面向接口编程的区别、抽象类和接口的区别等

    文章目录 1. Java创建对象有哪四种方式? 2. 什么是maven?maven的核心功能有哪些? 3. 什么是MVC?说说分层的好处. 4. Spring的两大核心技术是什么? 5. 什么是IOC ...

  3. 不允许使用抽象类类型的对象怎么办_Java基础——面试官:你来说说抽象类和接口的区别...

    无论你是新手Java 程序员,还是老手程序员,可能在实际开发中很少自己写抽象类. 但是抽象类在某些时候的功能很强大,可以保证子类中百分百实现父类中的方法 -- 普通类的弊端,消除子类的冗余代码 -- ...

  4. C#中抽象类和接口的区别

    一.抽象类:       抽象类是特殊的类,只是不能被实例化:除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法,这是普通类所不能的.抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆 ...

  5. C#中抽象类和接口的区别与使用

    一.抽象类: 抽象类是特殊的类,只是不能被实例化:除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法,这是普通类所不能的.抽象方法只能声明于抽象类中,且不包含任何实现,派生类必须覆盖它们.另外 ...

  6. C#抽象类与接口的区别【转】

    C#抽象类与接口的区别[转] 一.抽象类:      抽象类是特殊的类,只是不能被实例化(可以用派生类实例化基类对象):除此以外,具有类的其他特性:重要的是抽象类可以包括抽象方法(当然它可以有普通方法 ...

  7. java 抽象类与接口区别是什么_JAVA中抽象类与接口的区别,分别在什么情况下使用它们...

    在网上看到很多人问关于"抽象类与接口的区别",因此本人想通过自己多年对JAVA开发的经验来总结一下抽象类与接口的区别以及分别在什么情况下使用它们. 在Java语言中, abstra ...

  8. C#中抽象类和接口的区别与应用场景

    一直对这两个基础概念没有很好的认识,在网上发现一个我能够理解的基础概念理解,存一下,反复看. C#中抽象类和接口的区别与应用场景一. 1.抽象类:抽象类是特殊的类,只是不能被实例化;和普通类一样里面什 ...

  9. 抽象类与接口的区别,以及使用依据

    对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方. 抽象类与接口 前言 一.语义上的区别 1.抽 ...

  10. 初学Java基础学习——抽象类和接口的区别

    初学Java基础学习--抽象类和接口的区别 一.关键字 1)抽象类的关键字 abstract class A{//定义一个抽象类: } class Test extends A{//子类使用exten ...

最新文章

  1. 初窥runtime的作用
  2. java 接口的泛型方法_Java泛型/泛型方法/通配符/泛型接口/泛型泛型擦出
  3. 创建磁盘陈列(RAID5)使用经验与原则
  4. r语言中mpg数据_R语言常用的数据处理的包(1)
  5. ZK的实际应用:MVVM –加载和渲染数据
  6. html页面tableview,用JS写的一个TableView控件代码
  7. mssql 无法启动调试器 数据为空_Windows无法启动:如何利用PE拯救桌面重要数据?...
  8. Python面向对象程序设计中属性的作用与用法
  9. zookeeper注册中心 kerberos_ZooKeeper 并不适合做注册中心
  10. vue 判断权限过期_vue 路由权限
  11. 简易CPU的C++实现
  12. 白帽子讲Web安全(第一章总结)
  13. 【C语言】充当右值时,数组名前加不加的区别
  14. python进行谱曲_python创作音乐_ 计算机创作,计算音乐
  15. C语言递归之苹果分盘问题
  16. 蓝牙解码格式哪个最好_可能是声音最好的蓝牙解码耳放之一,Oriolus 1795 体验...
  17. Android 面试技巧分享~
  18. 1688搜索新品API接口-(按关键字搜索新品数据API接口)
  19. 科大讯飞语音识别_科大讯飞 语音识别_科大讯飞语音识别系统 - 云+社区 - 腾讯云...
  20. 小孙讲认证:ASTM F963 -17 CPSIA CPC 适用产品:婴儿磨牙产品、牙胶

热门文章

  1. 概率论与数理统计 | (16) 方差分析与一元线性回归
  2. php codeigniter3,从CodeIgniter 3系列版本升级到4系列版本
  3. php codeigniter ext,PHP CodeIgniter框架源码解析
  4. windows蓝屏解决方式SYSTEM_THREAD_EXCEPTION_NOT_HANDLED,失败的操作wdf01000.sys
  5. 计算机软考深圳积分,2020年软考证书能为深圳积分入户加分吗?
  6. C语言编程>第二周 ① 打印菱形图案
  7. C++内存分配(operator new)
  8. 连接/映射网络位置/共享磁盘
  9. html,css使用表格制作课程表
  10. 分析完百年飞机空难数据,我发现了这几条“保命”小秘诀