Questions tagged [abstract-class]

An abstract class is a class that cannot be instantiated. They are generally meant to be extended/subclasses and generally have "abstract methods" that must be implement by subclasses.

What is an abstract class?

The exact definition varies depending on the language, but generally an abstract class:

  • Cannot be directly instantiated
  • Is meant to be subclassed
  • Can define abstract methods, which must be implemented in subclasses.

Abstract classes in different programming languages

Java

An abstract class is usually used as a base class for a class hierarchy, where subclasses need to define custom behavior for certain methods.

See:

C++

Abstract classes (also called interfaces) are made by defining at least one pure virtual function. The subclasses are required to override the pure virtual functions.

See:

Tag usage

Use this for questions about how abstract classes are related to software engineering. Do not use this for general programming questions.

125 questions
72
votes
7 answers

When to use abstract classes instead of interfaces with extension methods in C#?

"Abstract class" and "interface" are similar concepts, with interface being the more abstract of the two. One differentiating factor is that abstract classes provide method implementations for derived classes when needed. In C#, however, this…
Gulshan
  • 9,402
  • 10
  • 58
  • 89
47
votes
4 answers

In C++ why and how are virtual functions slower?

Can anyone explain in detail, how exactly the virtual table works and what pointers are associated when virtual functions are called. If they are actually slower, can you show the time that the virtual function takes to execute is more than normal…
MdT
  • 589
  • 1
  • 5
  • 6
43
votes
9 answers

Why should I declare a class as an abstract class?

I know the syntax, rules applied to abstract class and I want know usage of an abstract class Abstract class can not be instantiated directly but can be extended by other class What is the advantage of doing so? How it is different from an…
Vaibhav Jani
  • 535
  • 1
  • 5
  • 11
31
votes
2 answers

Implementation of pure abstract classes and interfaces

Although this isn't mandatory in the C++ standard, it seems the way GCC for example, implements parent classes, including pure abstract ones, is by including a pointer to the v-table for that abstract class in every instantiation of the class in…
Clinton
  • 1,053
  • 1
  • 11
  • 14
23
votes
4 answers

Never make public members virtual/abstract - really?

Back in the 2000s a colleague of mine told me that it is an anti-pattern to make public methods virtual or abstract. For example, he considered a class like this not well designed: public abstract class PublicAbstractOrVirtual { public abstract…
21
votes
4 answers

Use abstract class in C# as definition

As a C++ developer I'm quite used to C++ header files, and find it beneficial to have some kind of forced "documentation" inside the code. I usually have a bad time when I have to read some C# code because of that: I don't have that sort of mental…
21
votes
6 answers

What are the differences between abstract classes, interfaces, and when to use them

Recently I have started to wrap my head around OOP, and I am now to the point where the more I read about the differences between abstract classes and interfaces the more confused I become. So far, neither can be instantiated. Interfaces are more…
user66662
  • 289
  • 1
  • 2
  • 5
20
votes
2 answers

Abstract exception super type

If throwing System.Exception is considered so bad, why wasn't Exception made abstract in the first place? That way, it would not be possible to call: throw new Exception("Error occurred."); This would enforce using derived exceptions to provide…
marco-fiset
  • 8,721
  • 9
  • 35
  • 46
18
votes
5 answers

Do enums create brittle interfaces?

Consider the example below. Any change to the ColorChoice enum affects all IWindowColor subclasses. Do enums tend to cause brittle interfaces? Is there something better than an enum to allow for more polymorphic flexibility? enum class…
Matthew James Briggs
  • 1,757
  • 3
  • 15
  • 23
17
votes
4 answers

When to move a common field into a base class?

I currently have two derived classes, A and B, that both have a field in common and I'm trying to determine if it should go up into the base class. It is never referenced from the base class, and say if at some point down the road another class is…
samus
  • 465
  • 1
  • 5
  • 13
16
votes
5 answers

Why shouldn't static methods be able to be overrideable?

In answers to this question, the general consensus was that static methods are not meant to be overridden (and thus static functions in C# cannot be virtual or abstract). This is not only the case in C#, though; Java also forbids this and C++…
15
votes
3 answers

Should I implement an interface directly or have the superclass do it?

Is there a difference between public class A extends AbstractB implements C {...} versus... public class A extends AbstractB {...} abstract class AbstractB implements C {...} I understand that in both cases, class A will end up conforming to the…
c_maker
  • 8,250
  • 4
  • 35
  • 53
15
votes
6 answers

Can I consider interface methods as abstract methods?

I was thinking about that, and I had some doubts. When I declare an interface, for example: public interface MyInterface { public void method1(); public void method2(); } Could these interface methods be considered abstract? What I mean is…
rogcg
  • 385
  • 1
  • 3
  • 11
13
votes
5 answers

Does it make sense to define an interface if I already have an abstract class?

I have a class with some default/shared functionality. I use abstract class for it: public interface ITypeNameMapper { string Map(TypeDefinition typeDefinition); } public abstract class TypeNameMapper : ITypeNameMapper { public virtual…
Konrad
  • 1,529
  • 2
  • 17
  • 32
13
votes
4 answers

Is there a different usage rationale for abstract classes/interfaces in C++ and Java

According to Herb Sutter one should prefer abstract interfaces (all pure virtual functions) to abstract classes in C++ to decouple the implementation as far as possible. While I personally find this rule very useful, I have recently joined a team…
Martin
  • 273
  • 1
  • 6
1
2 3
8 9