4

I'm currently doing a lot of programming in C. I am an undergrad student. The first language we learned was Java and now I'm learning C.

In Java, we create a class and it's field variables (state) and a bunch of methods (or behaviours) for said instance of an object.

in C we have something very similar, we have a struct which contains our object (I guess, i consider this to be its 'state' or field variable or 'attrubutes') and we have functions that the object can be passed to to modify its behaviour.

In C I can achieve the same kind of 'encapsulation' and a form of 'inheritance' via data hiding or sharing via header files.

While I completely understand that Java and C go about achieving this in very different ways, I am confused as to why C is not really considered OOP but rather procedural.

I feel that I'm missing something important. Maybe I'm unclear of what procedural programming is? Or I'm just plan wrong or using C completely wrong. Would be good to know.

EDIT:

Rather than typing to each poster, I'll just add it here. Thank you all so much, I honestly thought I was missing some important concept. The posts you all made make sense and I feel like I have a better understanding. Thank you all very much for answering so quickly and taking the time to reply to this post.

Thanks again I really appreciate it!

Daniel
  • 49
  • 2
  • 1
    This has already been answered [here](http://programmers.stackexchange.com/questions/113533/why-is-c-not-considered-an-object-oriented-language). – mbass Dec 14 '15 at 09:44
  • 1
    @YoungHobbit Or, everything is an object in C. Also, classes don't define objects. Objects are instances of classes (or types, depending on the language) – juanchopanza Dec 14 '15 at 09:44
  • Related: http://stackoverflow.com/questions/351733/can-you-write-object-oriented-code-in-c – haccks Dec 14 '15 at 13:51

3 Answers3

7

You can do object-oriented programming in a procedural language (or almost any language), it's just harder, because you have to do more of the plumbing yourself.

Java is considered an OOP language (by most people) because it provides direct support for OOP concepts like encapsulation, inheritance, and polymorphism.

C isn't considered an OOP language because while you can do encapsulation, inheritance, and polymorphism with it, the language doesn't provide constructs to help you do so.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
1

Yes, we can achieve the functionalities of Java or any other language for which they are known for in C however the only major point to note is ease of usability. Those languages made it much easier to use concept of encapsulation, inheritance etc. Biggest difference in the two languages arise due to the use of different programming paradigms. C breaks down to functions while JAVA breaks down to Objects. C is more procedure-oriented while JAVA is data-oriented.

0

Of course, you can use object-oriented programming techniques in any language (even with pure machine code). The difference is that in Java objects are enforced, while in C they are merely one of possible conventions (that you may choose to follow, or ignore).

Petr Tesařík
  • 104
  • 1
  • 5