What is the difference between a concrete class and an abstract class?

C++OopClass

C++ Problem Overview


I am learning C++, but I am confused about abstract class and concrete class. Some real world examples would be appreciated.

C++ Solutions


Solution 1 - C++

An abstract class is a class for which one or more methods are declared but not defined, meaning that the compiler knows these methods are part of the class, but not what code to execute for that method. These are called abstract methods. Here is an example of an abstract class.

class shape {
public:
  virtual void draw() = 0;
};

This declares an abstract class which specifies that any descendants of the class should implement the draw method if the class is to be concrete. You cannot instantiate this class because it is abstract, after all, the compiler wouldn't know what code to execute if you called member draw. So you can not do the following:

shape my_shape();
my_shape.draw();

To be able to actually use the draw method you would need to derive classes from this abstract class, which do implement the draw method, making the classes concrete:

class circle : public shape {
public:
  circle(int x, int y, int radius) {
    /* set up the circle */
  }
  virtual draw() {
    /* do stuff to draw the circle */
  }
};

class rectangle : public shape {
public:
  rectangle(int min_x, int min_y, int max_x, int max_y) {
    /* set up rectangle */
  }
  virtual draw() {
    /* do stuff to draw the rectangle */
  }
};

Now you can instantiate the concrete objects circle and rectangle and use their draw methods:

circle my_circle(40, 30, 10);
rectangle my_rectangle(20, 10, 50, 15);
my_circle.draw();
my_rectangle.draw();

Now of course the question is, why would you want to do this? Couldn't you just as well have defined the circle and rectangle classes and have done away with the whole shape class? You could, but then you wouldn't be able to take advantage of their inheritance:

std::vector<shape*> my_scene;
my_scene.push_back(new circle(40, 30, 10));
my_scene.push_back(new rectangle(20, 10, 50, 15));
std::for_each(my_scene.begin(), my_scene.end(), std::mem_fun_ref(&shape::draw)

This code let's you collect all your shapes into one container. This makes it a lot easier if you have a lot of shapes and many different shapes in your scene. For example we can now draw all the shapes in one go, and the code that does so doesn't even need to know about the different types of shapes we have.

Now finally we need to know why the draw function of shape is abstract, and not just an empty function, i.e. why didn't we just define:

class shape {
public:
  virtual void draw() {
    /* do nothing */
  }
};

The reason for this is that we don't really want objects of type shape, they wouldn't be real things anyway, they would be abstract. So it doesn't make any sense to define an implementation for the draw method, even an empty one. Making the shape class abstract prevents us from mistakenly instantiating the shape class, or mistakenly calling the empty draw function of the base class instead of the draw function of the derived classes. In effect we define an interface for any class that would like to behave like a shape, we say that any such class should have a draw method that looks like we have specified it should.

To answer you last question, there isn't any such thing as a 'normal derived class' every class is either abstract or concrete. A class that has any abstract methods is abstract, any class that doesn't is concrete. It's just a way to differentiate the two types of classes. A base class can be either abstract or concrete and a derived class can be either abstract or concrete:

class abstract_base {
public:
  virtual void abstract_method1() = 0;
  virtual void abstract_method2() = 0;
};

class concrete_base {
public:
  void concrete_method1() {
    /* do something */
  }
};

class abstract_derived1 : public abstract_base {
public:
  virtual void abstract_method3() = 0;
};

class abstract_derived2 : public concrete_base {
public:
  virtual void abstract_method3() = 0;
};

class abstract_derived3 : public abstract_base {
public:
  virtual abstract_method1() {
    /* do something */
  }
  /* note that we do not provide an implementation for
     abstract_method2 so the class is still abstract */
};

class concrete_derived1 : public concrete_base {
public:
  void concrete_method2() {
    /* do something */
  }
};

class concrete_derived2 : public abstract_base {
public:
  virtual void abstract_method1() {
    /* do something */
  }
  virtual void abstract_method2() {
    /* do something */
  }
  /* This class is now concrete because no abstract methods remain */
};

Solution 2 - C++

Abstract class can not be used to create an object. Whereas, concrete class can be used to create an object.

Concrete means''existing in reality or in real experience; perceptible by the senses; real''. Whereas, abstract means 'not applied or pratical; theoritical'.

An abstract class can't be instantiated. Whereas, a concrete one can.

An abstract class is one that has one or more pure virtual function. Whereas a concrete class has no pure virtual functions.

Solution 3 - C++

A concrete class is a class that can be used to create an object. An abstract class cannot be used to create an object (you must extend an abstract class and make a concrete class to be able to then create an object).

Pretend that there is a machine that can "stamp" raw materials and make a car. The stamper is a concrete class. From this we can create car objects. An abstract class would be the blueprints for the stamper. You cannot make cars from the blueprints of the stamper, you need to make the stamper class first from the blueprints.

Solution 4 - C++

An abstract class can't be instantiated whilst a concrete one can. An abstract class serves as "blueprint" for derived classes, ones that can be instantiated.

E.g. Car class (abstract) whilst Audi S4 class (deriving from Car) class is a concrete implementation.

Solution 5 - C++

Solution 6 - C++

A good example of using an abstract class is when you're building something very modular. Let's say you're working with a data store, but that data could be in a MySQL database, SQLite database, XML file, or plain text. To retain this versatility in your code, you can create a class AbstractDatastore that defines the public methods you want to use to get information from the datastore. Then you create your specific implementations of the AbstractDatastore, such as XmlDatastore, SQLiteDatastore, etc. Then, your program just needs knows it's getting an AbstractDatastore and that it must have those functions defined in AbstractDatastore but it doesn't know or care how the data is stored or retrieved.

Solution 7 - C++

C++ Faq Lite is an excellent site to look for answers on this kind of questions.

> At the design level, an abstract base class (ABC) corresponds to an abstract concept. If you asked a mechanic if he repaired vehicles, he'd probably wonder what kind-of vehicle you had in mind. Chances are he doesn't repair space shuttles, ocean liners, bicycles, or nuclear submarines. The problem is that the term "vehicle" is an abstract concept (e.g., you can't build a "vehicle" unless you know what kind of vehicle to build). In C++, class Vehicle would be an ABC, with Bicycle, SpaceShuttle, etc, being derived classes (an OceanLiner is-a-kind-of-a Vehicle). In real-world OO, ABCs show up all over the place

An Abstract Class is a class that has one or more pure virtual member functions. You cannot make an object (instance) of an Abstract Class

 class Shape {
 public:
   virtual void draw() const = 0;  // = 0 means it is "pure virtual"
   ...
 }; 

Solution 8 - C++

Base-class vs. derived class is an orthogonal concept to abstract class vs. concrete class.

A base class is one that does not inherit from any other class. A derived class does inherit from another class.

An abstract class is one that has one or more pure virtual functions. A concrete class has no pure virtuals.

An abstract class can be either a base class or a derived class (it is derived from another abstract class). A concrete class can also be either base or derived. You can even derive an abstract class from a concrete class, by adding a pure virtual function to the derived class. But in general use, there is one base abstract class and one or more concrete derived classes.

Solution 9 - C++

Concrete class has all its method implemented. Abstract class all its method except some (at least one) method(s) un-implemented so that you can extend it and implement the un-implemented method.

Advantage : By extending from abstract class you get all functionality of base class & you will be 'forced' to implement the un-implemented method. So the designer of the class is basically forcing you to write code in the abstract method before the class is of any use to you.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncoming out of voidView Question on Stackoverflow
Solution 1 - C++wichView Answer on Stackoverflow
Solution 2 - C++Th. Manjit SEMCView Answer on Stackoverflow
Solution 3 - C++Jansen PriceView Answer on Stackoverflow
Solution 4 - C++jldupontView Answer on Stackoverflow
Solution 5 - C++Kelly S. FrenchView Answer on Stackoverflow
Solution 6 - C++Corey DView Answer on Stackoverflow
Solution 7 - C++Diego DiasView Answer on Stackoverflow
Solution 8 - C++KeithBView Answer on Stackoverflow
Solution 9 - C++prashantView Answer on Stackoverflow