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

前端 未结 7 2158
再見小時候
再見小時候 2021-01-04 13:29

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 atleas
相关标签:
7条回答
  • 2021-01-04 13:36

    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.

    0 讨论(0)
  • 2021-01-04 13:37

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

    0 讨论(0)
  • 2021-01-04 13:41

    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.

    0 讨论(0)
  • 2021-01-04 13:46

    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.

    0 讨论(0)
  • 2021-01-04 13:52

    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.

    0 讨论(0)
  • 2021-01-04 13:52

    Only Chuck Norris can instantiate an abstract class.

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

    0 讨论(0)
提交回复
热议问题