10

I found in many places that :

  • An Abstract Class is a class which is supposed to be used as a base class.
  • An Abstract Class is a class which has atleast one Pure Virtual Function.

But one thing that always strikes my mind is why can't we create an instance of an abstract class? Many places on the Internet say there is no point in creating an instance, or some say that they are supposed to be used as base classes. But why is it an error to create an instance of an abstract class?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Vijay
  • 65,327
  • 90
  • 227
  • 319
  • 15
    If I ask you to draw a picture of a cat then you can easily draw a picture of a cat (if you are good in drawing : )) ,but if I ask you to draw a picture of an animal, can you draw it? you may say exactly which animal? Similarly you can create an object of concrete class Cat, but you can't create an object of abstract class Animal. The whole idea behind abstract class is code re-usability so that common behavior of a Cat class and Dog class can be implemented inside Animal class. – Searock Feb 27 '11 at 05:19
  • 2
    Although C++ forbids creation of abstract classes, you might be interested to know that Delphi does not. The compiler will warn you if you attempt it, but it will compile, and the instantiation will run just fine. There are even trivial ways to avoid the warning. Invoking an abstract method will raise an exception, though. – Rob Kennedy Feb 27 '11 at 05:38
  • @Rob: Interesting that Delphi works that way. Is there any particular advantage to being able to instantiate an abstract class (i.e., have you ever used it)? And how does it call a constructor at run-time? – Cody Gray - on strike Feb 27 '11 at 09:33
  • @Cody, there's no use case. Allowing it is a requirement of not being able to prevent it. Delphi allows *class references*, where a variable represents the class itself, and you can call (non-instance) methods on the variable. It's perfectly legitimate to store a reference to an abstract class — the compiler doesn't know at assignment time whether you'll call abstract methods on it — so it can't prohibit assignment. Among the methods you can call is the constructor. Since the compiler can't prevent indirect instantiation of abstract classes, it'd be pointless to prohibit direct instantiation. – Rob Kennedy Feb 27 '11 at 09:51

7 Answers7

17

Your void bar()=0; is not valid -- the =0 notation can only be used with virtual functions.

The whole point of an abstract class is that it's abstract -- you've defined an interface but not an implementation. Without an implementation, instantiating the class wouldn't produce a meaningful or useful result. If it does/would make sense to instantiate objects of that class, then you simply don't want to use an abstract class in the first place.

For example, consider device drivers. We might have a driver for an abstract storage device. We define some capabilities for that device, such as reading and writing data. That abstract class gives any code that wants to read/write data the ability to work with an concrete class that derives from that abstract class.

We can't just instantiate our abstract storage device though. Instead, we need a concrete object like a thumb drive, disk drive, etc., to actually read from/write to. The concrete class is needed because we need code specific to the actual device to carry out the commands we've defined in our abstract base. Our abstract storage class just has a read or write, but do the reading or writing, we need a driver for a specific device. One might know how to talk to a SATA hard drive, while another knows how to talk to a USB thumb drive and a third knows how to read from or write to an SD card. We can't, however, just say "I'm going to create an abstract storage device", and talk to it without defining the actual code that will translate a "write" command into (for example) the right signals going over SATA, USB, Firewire, etc., to get the data onto a real drive.

As such, attempting to instantiate our abstract class makes no sense, and isn't allowed. We just use the abstract base class so the rest of the system can deal with all devices uniformly. The rest of the code doesn't care how the signals are different from each other -- it just sees a bunch of disk drives, and can work with all of them, even though the details of reading data over USB are completely different from reading over Firewire (for example).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
12

An abstract class represents something that isn't specific enough to be instantiated. For instance, what if someone asked you to create a vehicle? You'd have to ask, "what kind of vehicle?" You wouldn't know whether to create a car, a sled, or a space shuttle. There's no such object as a "vehicle". Yet "vehicle" is a useful abstraction that can be used to group objects, indicating common behaviors among them. That's what abstract classes are for.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
4

An abstract class is more than an interface. It may have data members. It may have member functions that are not pure virtual, or non-virtual at all. Even a pure virtual function may have a body, providing a default implementation. So this is not about a physical impossibility of instantiating an abstract class.

The main point is that a pure virtual function is a virtual function that must be overridden by a derived class. That means that a derived class must be defined, and the way to force that is to forbid the instantiation of an abstract class.

An abstract class is not specific enough to be instantiated. Not necessarily because it is missing a definition of a function, because it may not be missing it. It is not specific enough because it represents an abstract concept, which must be made more specific before it can be instantiated.

Dima
  • 38,860
  • 14
  • 75
  • 115
  • This is incorrect. A virtual function may have a body. By definition, a "pure" virtual function is one that is defined as " = 0" (i.e., one that does not provide a definition). – Michael Aaron Safyan Feb 27 '11 at 11:20
  • 2
    With all due respect, it is you, who are incorrect. In C++ a pure virtual function may have a body. http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation – Dima Feb 27 '11 at 14:19
3

That's the whole point of an abstract class: that some details must be provided by the implementor.

Think about it: what would be the point of marking a class as abstract if you could instantiate it directly? Then it would be no different than any other class.

harpo
  • 41,820
  • 13
  • 96
  • 131
2

The reason an abstract class cannot be instantiated is: what do you do if you execute the pure virtual function? That would be a serious error, and it's better to catch that at compile-time than at runtime.

Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • Actually, a pure virtual function may have a body. Still, it must be overridden by a derived class. Not being able to instantiate an abstract class is less about the physical impossibility of doing so, and more about forcing the pure virtual functions to be overridden. – Dima Feb 27 '11 at 05:14
  • 1
    You can execute a pure virtual function that's been defined without any errors or anything. – Eugen Constantin Dinca Feb 27 '11 at 05:16
  • @Dima, if a virtual function has a body, it is not "pure". – Michael Aaron Safyan Feb 27 '11 at 11:17
  • @Eugen, you cannot execute a pure virtual function (at runtime), though you can create a function that invokes another function that is pure virtual in the current scope (but that invocation actually calls an override in a derived class which provides an implementation for that function). – Michael Aaron Safyan Feb 27 '11 at 11:18
  • @Michael Aaron Safyan: In Java and C# pure virtual methods (denoted by 'abstract' keyword) cannot have a body. However, in C++, a pure virtual member function can have a body. You do not see it often, but it is allowed by the language. See http://stackoverflow.com/questions/2089083/pure-virtual-function-with-implementation – Dima Feb 27 '11 at 14:24
  • 1
    @Michael: Something like this is perfectly legal: `struct Base { virtual void foo() = 0 {...}}; struct Derived : Base { void foo(){ Base::foo(); } };`. – Eugen Constantin Dinca Feb 27 '11 at 16:18
  • @Eugen: Almost. If a pure virtual function has a definition, then it has to be outside the class. – Dima Feb 27 '11 at 19:49
1

In abstract class no method definition is given, only structure is provided. If we could instantiate abstract class and call those method, it will be a huge mess. Abstract class is use to maintain a design pattern of the code.

Abdullah Md. Zubair
  • 3,312
  • 2
  • 30
  • 39
  • 2
    Not true. A pure virtual function can have a definition. And an abstract class can have non-pure virtual functions or non-virtual functions. – Dima Feb 27 '11 at 05:25
0

Only Chuck Norris can instantiate an abstract class.

https://api.chucknorris.io/jokes/ye0_hnd3rgq68e_pfvsqqg

Bulletmagnet
  • 5,665
  • 2
  • 26
  • 56