-1

As per definition of Abstraction, hiding implementation of internal code and this can be achieved by OOPs language by providing class object where methods and attributes are bind to that object. My question is... in C language if write some function and share its prototype and return type to developers for usage then by this also I am able to achieve Abstraction.

Then why Abstraction is feature of OOPs and not of procedure oriented language??

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 7
    You can write object-oriented code in C. It's true that there's no special syntax for it, but OOP is a set of ideas, not a specific syntax or set of keywords. – larsks Jun 28 '21 at 20:33
  • No. There is nothing in the language itself. You can workaround as *larsks* suggested, but if you want object programming - use OO language like C++. – 0___________ Jun 28 '21 at 20:36
  • 1
    If I am right, the first version of C++ was written in C, using macros. –  Jun 28 '21 at 20:38
  • Who is voting to close this as "primarily opinion based"? Questions about technical definitions are perfectly valid here. – skrrgwasme Jun 28 '21 at 20:53
  • 2
    @YvesDaoust [Cfront](https://en.wikipedia.org/wiki/Cfront), the first C++ implementation, was a compiler that generated C code as output. – Keith Thompson Jun 28 '21 at 21:27
  • 1
    @skrrgwasme Not me yet, but I'm tempted. *Why is abstraction a feature of OOP and not of procedural languages* is open to a lot of interpretation and potential debate. Certain types of abstraction are clearly designed-in features of languages like C++, Java, and Swift; C doesn't have any specific support for abstraction even if you can use its building blocks to get there (sort of). On the other hand, *every* computer language including assembly is a sort of abstraction that hides details. Also, the question is based on an unfounded assumption about procedural languages. – Caleb Jun 28 '21 at 21:28
  • Programming abstractions is a very old concept, and C was designed to allow for them to be used. C was the implementation language for UNIX, which is chock full of examples of abstractions. https://stackoverflow.com/q/17621544/315052 – jxh Jun 28 '21 at 21:51
  • @KeithThompson at the end of any compiler is machine code which does not make the assembler the OO language. Wrong logic. When I was a student I wrote ADA cross compiler giving the C output. Does it make C Ada like language? Of course not – 0___________ Jun 28 '21 at 22:04
  • 1
    @0___________ It sounds like there was something in my comment that you disagreed with, but I don't know what it might have been. I was replying to a previous comment, not to the question. – Keith Thompson Jun 28 '21 at 22:23
  • Streams are a pretty good example of how C does abstraction. The `FILE` type keeps track of all the information associated with an I/O channel, but its implementation is not exposed to you - instead of manipulating `FILE` objects directly, you use the `stdio` API to open, read, write, and check the status of streams. You don’t have to know anything about I/O channels on x86 vs. SPARC vs. MIPS vs. Power - that’s all hidden behind the API. – John Bode Jun 28 '21 at 23:10
  • thanks@larks, this shows that my understanding was correct... – Shashank v Jun 29 '21 at 09:24

2 Answers2

3

It sounds like you're working from an overly-constrained definition of "abstraction."

Your conclusion that "abstraction" is attainable by procedural programming languages is correct. Drivers written in C provide an abstraction that allows an OS to interact with hardware without knowing the details. Python abstracts the implementation details of a dictionary (implemented in C) to allow a programmer to use them easily. Pedals and steering wheels provide a simple abstraction to operate a motor vehicle.

Abstraction is not at all limited to OOP, it is just one aspect of it that is heavily emphasized when OOP is taught.

If you're learning from reference materials that provide a "definition of abstraction" that implies that it only applies to OOP, I suggest finding a new resource because that is not correct.

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
2

Even though C isn't designed to support object-orient programming, it is still possible to write OOP in C (or more accurately - implement OOP concepts using C features).

One common way is by using structs as "objects" - Data Abstraction in C (@jacwah)

Look at the struct from the Linux kernel source below (from include/linux/virtio.h).

 * virtio_driver - operations for a virtio I/O driver
 * @driver: underlying device driver (populate name and owner).
 * @id_table: the ids serviced by this driver.
 * @feature_table: an array of feature numbers supported by this driver.
 * @feature_table_size: number of entries in the feature table array.
 * @probe: the function to call when a device is found.  Returns 0 or -errno.
 * @remove: the function to call when a device is removed.
 * @config_changed: optional function to call when the device configuration
 *    changes; may be called in interrupt context.
 */
struct virtio_driver {
        struct device_driver driver;
        const struct virtio_device_id *id_table;
        const unsigned int *feature_table;
        unsigned int feature_table_size;
        int (*probe)(struct virtio_device *dev);
        void (*scan)(struct virtio_device *dev);
        void (*remove)(struct virtio_device *dev);
        void (*config_changed)(struct virtio_device *dev);
#ifdef CONFIG_PM
        int (*freeze)(struct virtio_device *dev);
        int (*restore)(struct virtio_device *dev);
#endif
};

Another way is by naming conventions & correct usage of modules - http://rodrigomendez.me/writing-object-oriented-code-in-c/

Also look at (Referencing a book about the subject) - How would one write object-oriented code in C?

Y.R.
  • 371
  • 1
  • 7