`
kingquake21
  • 浏览: 268983 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java中Abstract class与interface的差别

阅读更多

对于语法差别,很多文章都写了,就不再赘述了,这儿主要讲本质的差异

抽象类定义了其子类的核心特征和功能(is a),例如继承Thread通常表明该类“is a”线程

而接口只是定义了类的附加能力(-able/can-do),例如Runable表示可以单独运行的任务,但是并不是说该类is a线程;Comparable表示类具有比较的功能,但是并不是说该类"is a"比较器

如果需要为子类添加或修改默认的行为,此时应该选择抽象类,而不是接口

如果需要为不相干的类提供公共的功能,应该使用接口;如果建立的模型在层次上很接近,应该选用抽象类

Interfaces vs Abstract Classes feature interface abstract class multiple inheritance default implementation constants third party convenience is-a vs -able or can-do plug-in homogeneity maintenance speed terseness adding functionality
A class may implement several interfaces. A class may extend only one abstract class.
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.
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.
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.
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
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.
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 .
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 .
Slow, requires extra indirection to find the corresponding method in the actual class. Modern JVMs are discovering ways to reduce this speed penalty. Fast
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 .
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.
分享到:
评论

相关推荐

    java中abstractclass和Interface.doc

    ### Java中的Abstract Class与Interface详解 #### 一、面向对象设计的重要性及抽象的概念 面向对象设计的核心在于抽象,这是衡量软件设计质量的关键因素之一。良好的软件设计不仅体现在高效的底层实现上,更重要的...

    深入理解abstract class和interface

    abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制,正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性...

    abstract class和interface有什么区别?

    ### abstract class和interface有什么区别? 在面向对象编程中,`abstract class` 和 `interface` 都是用来实现抽象化的工具,但它们之间存在着重要的差异。理解这些差异有助于开发者更有效地设计和实现软件系统。 ...

    java abstract class interface之间的区别介绍

    在Java编程语言中,抽象类(abstract class)和接口(interface)都是用于实现抽象化的重要工具,它们可以帮助程序员更好地组织代码,提高代码的可复用性和扩展性。尽管二者在功能上有一些相似之处,但在实际使用中...

    Java abstract class 与 interface对比

    在Java编程语言中,`abstract class`(抽象类)和`interface`是两种重要的机制,它们用于实现多态性和抽象性。虽然它们都可以用来定义抽象方法,但它们之间存在显著的差异,这些差异影响了开发者在设计软件时的选择...

    [转]深入理解abstract class和interface

    在编程领域,`abstract class`(抽象类)和`interface`是两种重要的设计模式,它们在构建可扩展和模块化的软件系统中起着至关重要的作用。这篇文章将深入探讨这两个概念,以及它们在实际开发中的应用。 首先,让...

    Java中抽象类和接口的区别

    在Java语言中,abstract class和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,...

    interface&abstract class

    ### Interface与Abstract Class的区别及应用场景 #### 一、Interface与Abstract Class的概念理解 在面向对象编程语言中,如Java,接口(Interface)和抽象类(Abstract Class)是两种非常重要的概念,它们都被用来...

    Java抽象类和借口的区别.pdf

    abstract class和interface都是Java语言中支持抽象类定义的机制,但它们之间有很大的区别。选择合适的机制需要根据问题领域的特点和设计理念,正确地理解和使用abstract class和interface是软件设计和开发的关键。

    Java中interface接口与abstractclas

    在Java编程语言中,`interface`接口和`abstract class`抽象类是两种重要的面向对象设计概念,它们都用于实现多态性,但有着显著的区别。理解这些差异对于编写可扩展和可维护的代码至关重要。 首先,接口是完全抽象...

    Java中抽象类和接口的区别.zip_java interf

    在Java语言中,abstract class和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的面向对象能力。abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,...

    Java中抽象类和接口的区别与作用详解

    ### Java中抽象类和接口的区别与作用详解 #### 一、引言 在Java编程语言中,`abstract class`和`interface`是两种用于定义抽象类的重要机制。这两种机制不仅支持了Java的强大面向对象特性,而且也在实际开发过程中...

    Java中的interface接口

    在Java编程语言中,接口(Interface)是一种定义行为规范的关键元素,它允许我们实现多继承。接口是完全抽象的,不包含任何方法的实现,只有方法的声明、常量定义以及默认方法。Java中的接口提供了以下核心知识点: ...

    Java程序开发中abstract 和 interface的区别详解

    在Java编程语言中,`abstract class`(抽象类)和`interface`都是用来定义抽象类的概念,它们在面向对象设计中扮演着重要的角色。抽象类主要用于表示一系列具有共同特征但又各自具有独特行为的具体类的抽象,而接口...

    Java常用设计模式(SingleTon、FactoryMethod、AbstractFactory)

    Java设计模式是面向对象编程...在阅读《Chapter1___Java常用设计模式(SingleTon、FactoryMethod、AbstractFactory)》的相关资料时,你可以更深入地学习这些模式的细节,包括适用场景、优缺点以及如何在实际项目中实现。

    JAVA中抽象类和接口的区别

    在Java语言中,abstract class 和interface 是支持抽象类定义的两种机制区别

Global site tag (gtag.js) - Google Analytics