0

When and Why to use abstract classes/methods?

When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.) Use an abstract class to define a common base class for a family of types. Use an abstract class to provide default behavior. Subclass only a base class in a hierarchy to which the class logically belongs.

I did not understand the explanation in the above quote. Please explain why should an abstract class be used for creating a class library?

Community
  • 1
  • 1
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
  • You'll have to ask them what they mean by "interface". – juanchopanza Mar 23 '17 at 07:56
  • If they deleted the phrase "in preference to an interface" from the quote, I'd agree entirely. I have *no* idea what it is supposed to mean with the phrase though. – Martin Bonner supports Monica Mar 23 '17 at 08:04
  • If you had properly followed the link the answer is associated with, you'd have seen that it's about C#, not C++. C# has interfaces and abstract classes and they are different things, whereas C++ only has abstract classes. Therefore, this question is tagged wrong. – kushy Mar 23 '17 at 08:07
  • @MartinBonner Can you assume that that phrase is not there and then explain the quote as an answer, please? – Aquarius_Girl Mar 24 '17 at 04:32
  • Thanks to @LmTinyToon who explained the "in preference to an interface" phrase - the quote applies to C# where the two terms are distinct. – Martin Bonner supports Monica Mar 24 '17 at 08:31
  • Are you asking "why provide access to your library in terms of a base class, and then implement the library with derived classes"? The short answer is "because it means you can change the implementation without clients having to change". The long answer is a blog post, not a SO answer. – Martin Bonner supports Monica Mar 24 '17 at 08:35
  • No, I am specifically asking about "why provide access to your library in terms of an "abstract" base class, and then implement the library with derived classes?" @MartinBonner – Aquarius_Girl Mar 24 '17 at 09:29
  • If the class is abstract, the user can't accidentally create a BaseClass object, and then pass you a reference to that, rather than to a proper ImplementationClass (there are other ways to prevent this, but none as direct). You also probably want to avoid as much state as possible in the base class, in order to be able to change the implementation later. – Martin Bonner supports Monica Mar 24 '17 at 09:37
  • @MartinBonner If you write that as an answer and put some more details in it , I will select it. – Aquarius_Girl Mar 24 '17 at 09:39

1 Answers1

2

You should read articles of such kind very carefully. As I Understood, the main part of article is dedicated to c# language. In terms of such language there is big difference between interfaces and abstract classes. For example, interfaces in terms of c# language is just set of "pure" virtual methods (they cannot have definition within interface, only classes can implement it). Interfaces cannot have constructors. Abstract classes can have constructors. Moreover, c# does not support multiple inheritance ( as opposite to c++ language). In such way, c# interfaces and abstract classes look very different than c++'s one

Alex Aparin
  • 4,393
  • 5
  • 25
  • 51