Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't we create an instance of an abstract class?

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?

like image 846
Vijay Avatar asked Feb 27 '11 05:02

Vijay


People also ask

Can we create an instance for abstract class?

Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.

Why can't we instantiate an abstract class in C#?

An abstract class cannot be instantiated. An abstract class may contain abstract methods and accessors. It is not possible to modify an abstract class with the sealed modifier because the two modifiers have opposite meanings.

Why we can not create the object of abstract class in C++?

Because it's abstract and an object is concrete. An abstract class is sort of like a template, or an empty/partially empty structure, you have to extend it and build on it before you can use it. abstract class has a protected constructor (by default) allowing derived types to initialize it.

Why would you create an abstract class if it can have no real instances Mcq?

Explanation: When an abstract class is defined, it won't be having the implementation of at least one function. This will restrict the class to have any constructor. When the class doesn't have constructor, there won't be any instance of that class.


4 Answers

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).

like image 54
Jerry Coffin Avatar answered Oct 14 '22 08:10

Jerry Coffin


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.

like image 36
Fred Larson Avatar answered Oct 14 '22 09:10

Fred Larson


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.

like image 21
Dima Avatar answered Oct 14 '22 10:10

Dima


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.

like image 39
harpo Avatar answered Oct 14 '22 08:10

harpo