在国外的一个论坛上看到一个关于抽象类和接口比较的帖子,其中有一篇感觉很有道理:
Interface versus Class Inheritance is pretty easy once you understand it.
There is a very easy way to distinguish the 2.
Class inheritance = implemenation inheritance.
What this means is that if you have a base object you inherit from, your new derived object will either contain or override the existing functionality of the base object. This is often referred to as "is a " relationship.
A Beagle IS A Dog.
Interface inheritance is a little different. It is basically like a contract and is usually referred to as a "has a " relationship. By agreeing to implement an interface, you agree to implement all of its members, and provide your own implementation. An Interface should describe what a class does, rather than what a class is.
A Beagle Has A Bark
One reason to use Interfaces over Implementations is versioning or upgradability.
Any changes you make to your base class (abstract class) could possibly break subclasses that are derived from that class. By implementing interfaces you alleviate this problem by allowing clients or code to have multiple interfaces. By using interfaces, subclass can only see its parents interface, not the implementation.
分享到:
相关推荐
What's the difference between TAF and FCF
Java面试题和答案(英文版),What is the difference between an Interface and an Abstract class
What are the differences between least-squares and Kalman filtering
1、Explain the difference between internal and external fragmentation. 2、Given five memory partitions of 100 KB, 500 KB, 200 KB, 300 KB, and 600KB (in order), how would each of the first-fit, best-...
What is the difference: between o(log n) and o(log n^2), where both log arithems have base 2 . a. o(log n^2) is bigger b. o(log n) is bigger c. no difference 3. For a class what would happen if we ...
43. What is the difference between the equilibrium approach and the no-arbitrage approach to modelling? 198 44. How good is the assumption of normal distributions for financial returns? 201 45. How ...
1. What is the IP address and TCP port number used by the client computer (source) that is transferring the file to gaia.cs.umass.edu? To answer this question, it’s probably easiest to select an ...
和...有什么区别 ...示例:“人与动物” 3-按Enter 4-它将在Google上搜索为“人与动物之间有什么区别” Github https://github.com/volkanakinpasa/what-is-the-different-between 支持语言:English
Nucleus C++ FILE is a C++ class interface into Nucleus FILE, a FAT16/FAT32 TM compatible file system specifically designed to work in embedded systems in conjunction with the Nucleus PLUS real-time ...
sva classical q&a Qi1)What is callback ? (Qi2)What is factory pattern ?...(Qi3)Explain the difference between data types logic and reg and wire . (Qi4)What is the need of clocking blocks ?
ChatGPT研究资料,What Is ChatGPT Doing ... and Why Does It Work (Stephen Wolfram) (Z-Library)
What's the Difference between Pneumatic, Hydraulic, and Electrical Actuators.docx
what is seen and what is not seen 一部很不错的书。
What the Corpus is, and why s u m m a r y A summary of the compression test results d e t a i l s More detailed results, including some statistical analysis c o r p o r a Descriptions of the various ...
SaaS vs PaaS vs IaaS What's The Difference and How To Choose(英文文档) pdf文件
What's the difference between an interface and an abstract class? Q30. What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access...
追溯性和分子通讯性能极限之间的特征结合是什么_What Is the Trait d'Union between Retroactivity and Molecular Communication Performance Limits.pdf
没有人预料到这一点——甚至包括它的创造者:ChatGPT作为一种能够以令人信服的人类水平进行写作的AI已经出现在舞台上。但是它真正的工作原理是什么?其“AI大脑”内部发生了什么?在这本短篇书籍中,杰出科学家和...
1-1 In UMTS,what’s the difference between AAL2 and AAL5? 1-2 In UMTS,what do FDD and TDD mean? 1-3 Describe the processor of hardover and soft handover? 1-4 Describe the spread mechanism used in UMTS...
max矩阵含义: What is the difference between GetNodeTM and GetObjectTM? How about LocalTm and WorldTM of a node?