在国外的一个论坛上看到一个关于抽象类和接口比较的帖子,其中有一篇感觉很有道理:
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.
分享到:
相关推荐
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
What is the difference between an abstract class and an interface? **Answer**: In Java, an abstract class can have both abstract and non-abstract (concrete) methods, whereas an interface can only ...
11. What is the difference between `System.out.println()` and `System.out.print()` in Java?Answer: Both methods are used for outputting text to the console, but they differ in how they display the ...
《ChatGPT在做什么...以及为什么它有效》 这本书由斯蒂芬·沃尔夫勒姆(Stephen Wolfram)撰写,探讨了ChatGPT的工作原理及其为何能成功运作。沃尔夫勒姆是计算科学领域的权威,他的公司Wolfram Media出版了这本书...
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...
A computer's architecture is its abstract model and is the programmer's view in terms of instructions, addressing modes and registers. A computer's organization expresses the realization of the ...
What is the difference between a task and a process? Do I need to worry about reentrancy? As we progress through Embedded Systems Firmware Demystified, you will come to see that these questions are ...
3) What is the difference between split and nosplit scheme? In general, splitting the image into four sets and looping on them is computational faster. However, in some cases like deconvolution, we ...
difference between ArrayList and LinkedList?` LinkedList and ArrayList are two different implementations of the List interface. LinkedList implements it with a doubly-linked list. ArrayList implements...
What is the difference between TC.EXE and TCC.EXE? A. The Turbo C package comes with two compilers, an Integrated Environment named TC.EXE and a command-line compiler named TCC.EXE. The Integrated...
本书详细介绍了数学的各个分支。对于有需要了解数学方法的程序员有很大的帮助。
WHAT IS A MATROID? JAMES OXLEY Abstract. Matroids were introduced by Whitney in 1935 to try to capture abstractly the essence of dependence. Whitney’s definition embraces a surprising diversity of ...
What is the matterPPT教案学习.pptx
What is the capital of EgyptPPT教案学习.pptx
(b) the name of the table, the names of the table's attributes, the data types of the table's attributes, the formats of the table's attributes, and the maximum number of rows that the table can have...
The C++ language has a long history, dating back to the 1980s. Recently it has undergone a renaissance, ...class and function templates, the difference between lvalue and rvalue references, and so on.
Q11) What is the difference between SEARCH and SEARCH ALL? A11) SEARCH - is a serial search.SEARCH ALL - is a binary search & the table must be sorted ( ASCENDING/DESCENDING KEY clause to be used & ...