C++ term.

An abstract class is a class containing at least one pure virtual function. As a result, no objects of the class can exist. One will have to derive classes from the abstract base class. These classes must then overload all the pure virtual functions of the base class in order not to be abstract as well.

Why bother?, you might ask. Well - I just started using the technique this week, after having programmed in C++ for 4 years. I use it, because: Skipping the first two statements above, I conclude that I obtain a more readable, more structured end class hierachy.
An abstract class (sometimes referred to as an abstract base class) is a class in C++ with at least one pure virtual function. A virtual function is a function that is declared in a base class and is then declared again in a derived class with the same parameter list. The virtual keyword is used to mark the function as virtual. Here is an example of a virtual function in a base class and its derived, along with a driver function that will show what happens.


#include 

using namespace std;

struct Base {
  virtual void foo();
};

struct Derived : Base {
  virtual void foo();
};

void Base::foo() {
  cout << "Base::foo() was called.\n";
}

void Derived::foo() {
  cout << "Derived::foo() was called.\n";
}

int main() {
  Base* b = new b;
  b.foo();
  delete b;
  b = new Derived;
  b.foo();
  delete b;
  return 1;
}

This program outputs -
Base::foo() was called.
Derived::foo() was called.

What, then, is a pure virtual function, and how do these fit into abstract classes? Pure virtual functions are simply functions that have no implementation in the base class, relying on derived classes to provide their own implementations. This means that no runtime instances (objects) can be created from the base class - these classes only exist to act as generic blueprints for other classes. This can, when used correctly, really help tidy up your class hierarchy.

You define a pure virtual functions like this -

virtual void foo() = 0;

Then derived classes simply declare the function as normal, ie. -

void foo();

Note that because you cannot create runtime instances of abstract classes, this will fail -


class Abstract { virtual void foo() = 0; };

int main() {
  Abstract a; // Error here.
}

It is, of course, perfectly legal to create pointers such as Abstract* a; for the purpose of dynamic runtime casting.

An abstract class (sometimes referred to as an abstract base class) is a class in C++ with at least one pure virtual function. A virtual function is a function that is declared in a base class and is then declared again in a derived class with the same parameter list. The virtual keyword is used to mark the function as virtual. Here is an example of a virtual function in a base class and its derived, along with a driver function that will show what happens.


#include 

using namespace std;

struct Base {
  virtual void foo();
};

struct Derived : Base {
  virtual void foo();
};

void Base::foo() {
  cout << "Base::foo() was called.\n";
}

void Derived::foo() {
  cout << "Derived::foo() was called.\n";
}

int main() {
  Base* b = new b;
  b.foo();
  delete b;
  b = new Derived;
  b.foo();
  delete b;
  return 1;
}

This program outputs -
Base::foo() was called.
Derived::foo() was called.

What, then, is a pure virtual function, and how do these fit into abstract classes? Pure virtual functions are simply functions that have no implementation in the base class, relying on derived classes to provide their own implementations. This means that no runtime instances (objects) can be created from the base class - these classes only exist to act as generic blueprints for other classes. This can, when used correctly, really help tidy up your class hierarchy.

You define a pure virtual functions like this -

virtual void foo() = 0;

Then derived classes simply declare the function as normal, ie. -

void foo();

Note that because you cannot create runtime instances of abstract classes, this will fail -


class Abstract { virtual void foo() = 0; };

int main() {
  Abstract a; // Error here.
}

It is, of course, perfectly legal to create pointers such as Abstract* a; for the purpose of dynamic runtime casting.

What

This syntactic element is common to most early-binding object-oriented computer programming languages such as C++, Java and Delphi.

An abstract class, often also called an abstract base class, is a class that cannot be instantiated due to the presence of one or more virtual functions that are declared but not implemented by the class, ie pure virtual methods. It's a way of telling yourself: Do not create create instances of this class, subclass it.

Code examples

In java, both classes and methods can be declared abstract, for e.g.
abstract class Polygon
{
   abstract public double getArea();
}
In Delphi (as with C++) methods can be declared abstract, and any class with abstract methods is an abstract class. for e.g.:
type
  TPolygon = class(TObject)
  public
    function GetArea: double; virtual; abstract;
  end;

See the other writeups for a C++ example. My C++ is too rusty, sorry.

But why would you want to do this?

A abstract method is a placeholder for functionality that the child class must fill in if it is to be concrete.

Making the method abstract says explicitly that the class is too generic to give a reasonable default implementation for the abstract method, and that all concrete child classes must override it in thier own way. A base class that is intended only for subclassing and not for direct use defines an interface contract that is shared by all of its child classes.

However, calling code can deal with an object that appears to be of the base type, and does not need to know how the method is implemented by the particular child class. Thus the calling code deals only with the interface contract defined by the base class.

Log in or registerto write something here or to contact authors.