Interfaces vs Abstract Classes...

原创 2004年10月06日 20:25:00

Interfaces vs Abstract Classes...

A side by side comparison of features of Interfaces and Abstract Classes.


By: Naveen spacer.gif Date: October 23, 2003spacer.gifspacer.gif <?XML:NAMESPACE PREFIX = ASP />spacer.gifPrinter Friendly Version
br>

Interface vs abstract class

When should you use an abstract class, when an interface, when both? This table will help you decide.

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 Damamation descendants are Dogs, they are not merely dogable. Implemented interfaces enumerate the general things a class can do, not the things a class is.
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. 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.
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 behavior, usually an abstract class works best.
Maintenance valign="top"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. 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.
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.

Abstract Classes Vs Interfaces

 IntroductionI think this is the most frequently question asked in interviews. This arcticles gives ...
  • fy050
  • fy050
  • 2008年09月01日 16:13
  • 308

[Java]--Abstract Class vs Interfaces

[Java]--Abstract Class vs Interfaces
  • beijingbuaaer
  • beijingbuaaer
  • 2016年08月20日 13:24
  • 122

Java Abstract class and Interface 实例化解析

//: Adventure.java // Multiple interfaces import java.util.*; interface CanFight { void fight(); ...
  • wangbofei
  • wangbofei
  • 2012年03月05日 11:14
  • 1698

interface vs abstract class 应用场景总结

1. interface的应用场合   A. 类与类之前需要特定的接口进行协调,而不在乎其如何实现。   B. 作为能够实现特定功能的标识存在,也可以是什么接口方法都没有的纯粹标识。   C. ...
  • tiercel2008
  • tiercel2008
  • 2012年06月26日 09:52
  • 639

Factory Method vs Abstract Factory

Factory Method vs Abstract Factory
  • u010373266
  • u010373266
  • 2016年12月20日 16:26
  • 314

[Java] Abstract classes vs. interfaces

When does it make sense to choose an abstract class over an interface? 什么时候应该使用抽象类比使用接口更好呢? 这篇文章将给...
  • lihe2008125
  • lihe2008125
  • 2011年02月16日 16:38
  • 365

Templates vs Interfaces

It seems another religion war has started between template fans ("Wrap, Wrap, Wrap") and virtual fun...
  • wishfly
  • wishfly
  • 2007年08月19日 09:21
  • 715

深入理解virtual,abstract class,interface

C++虚函数与JAVA中抽象函数比较,接口与抽象类,纯虚函数和虚函数的区别在于前者不包含定义,而后者包含函数体;abstract class和interface在Java语言中都是用来进行抽象类,语法...
  • nxh_love
  • nxh_love
  • 2013年12月13日 14:45
  • 7220

抽象类abstract和接口interface的区别与深入思考

在以前的编程过程中,经常对abstract和interface混淆,相信很多初学者都有这样的困惑,也问过很多经验丰富的程序员,他们也不能清楚地说出个所以然来。经过自己的思考,想到了一种比较形象的方式来...
  • wujiaxian
  • wujiaxian
  • 2014年10月11日 11:32
  • 2898

linux interfaces文件配置

1 auto lo   2 iface lo inet loopback   3   4 # The primary network interface   5 auto eth0   6 ...
  • mydear_11000
  • mydear_11000
  • 2015年11月12日 15:19
  • 789
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Interfaces vs Abstract Classes...
举报原因:
原因补充:

(最多只允许输入30个字)