4

I had a discussion about OO programming today and by browsing the internet I found a lot of different specifications for object oriented languages.

  • What are the requirements for a language to be object oriented? For myself an object oriented language must have classes, inheritance and encapsulation.
  • Is C an object oriented language just because you can use structs and program with an object oriented design? Why/ why not?

Are there any good sites/articles about this? And please, no Wikipedia links because I've already been there.

nist
  • 243
  • 2
  • 7
  • 6
    Who said that "object-oriented" is well-defined? –  Mar 19 '12 at 17:38
  • 4
    Smalltalk is the purest form of OOP – Raynos Mar 19 '12 at 17:39
  • 2
    This is at least 3 questions which could all be answered separately. You might want to consider asking three independent and better focused questions because someone trying to answer you might only be able to answer one or two of the many questions you ask. – FrustratedWithFormsDesigner Mar 19 '12 at 17:41
  • 1
    @Raynos: Alan Kay, the creator of Smalltalk, would probably disagree. He says that the important thing about OO is messaging, and that Smalltalk unfortunately overemphasizes objects and even more so classes over messages. In particular, he now seems to consider adding classes to Smalltalk a mistake. At least, his current project doesn't have classes. – Jörg W Mittag Mar 19 '12 at 22:55
  • @JörgWMittag: I'd be careful about putting words in Alan Kay's mouth. ;-) Objects are, at least from one perspective, tools to enable messaging. You need _something_ to hold state and behaviour, otherwise messaging is pointless. _How_ you use the objects and messages and where you put the emphasis is where Alan believes Smalltalk took a wrong turn from what I understand - feel free to educate me though ;-). I believe Raynos' point was that in Smalltalk, _everything is an object_ unlike, say, Java, where primitives, for instance, are not. – Amos M. Carpenter Mar 20 '12 at 00:54

6 Answers6

5

OO is usually "defined" in terms of four basic requirements:

  • Abstraction means you're able to separate an objects externally visible behavior from its implementation.

  • Encapsulation means being able to hide the details of the implementation of an object (so closely related to abstraction that some authors treat them as one, leaving only three major requirements).

  • Modularity means being able to package abstractions into discrete units.

  • Hierarchy is a ranking or ordering of abstractions -- an ability to define the relationship between one abstraction and another.


I, for one, would consider a language object oriented if it supports these four major requirements. Supports, however, goes a bit beyond "allows" or "tolerates".

C, for example, provides enough flexibility that you can simulate each of these reasonably well. C, however, provides only a little support for abstraction or modularity, and none at all for encapsulation or hierarchy -- you can do these things if you want to badly enough, but the language doesn't really provide any direct support for them.

C++, for an obvious comparison does provide direct support for each of these requirements. Just for example, encapsulation is directly supported by making class members private and/or protected and hierarchies are supported via class inheritance.

That's still not what would normally be called a "pure" object oriented language though. In a pure OO language (e.g., Smalltalk), you not only get support for hierarchy, but you get a pre-defined hierarchy of types, and you have to define the relationship of any new type to some existing type in that hierarchy (i.e., all your code most be in classes, and each class must derive from some existing one).

Although classes and inheritance are both common to many implementations of object orientation, neither is really necessary to it. Just for example, Self and JavaScript both use exemplar-based hierarchies. Instead of a hierarchy of classes, with each object an instance of a class, these just use a hierarchy of objects, with each based on some other object, but modified to some arbitrary degree.

Kromster
  • 468
  • 1
  • 6
  • 17
Jerry Coffin
  • 44,385
  • 5
  • 89
  • 162
  • @Giorgio: No, it doesn't -- not by itself. You need direct support for all four of those for object orientation. Any one by itself doesn't work. Ada 83 (for one example) supports three of the four (all but hierarchy). That's enough that it's typically called "object based", but not "object oriented". – Jerry Coffin Mar 19 '12 at 18:20
  • I deleted my question because after reading your answer I realised you state that encapsulation is a requirement, but not a feature that is unique to OO. Since you have answered, I repeat my question: I was doubting whether encapsulation identifies OO, since it is also present in procedural and functional languages. – Giorgio Mar 19 '12 at 18:26
  • It's not true that C does not provide direct support for hierarchy- it actually does. The C Standard specifies that a pointer to a struct may be converted to a pointer to it's first element by simply casting the pointer- which is hierarchical. – DeadMG Mar 19 '12 at 18:52
  • @DeadMG I think the implication is support for *object* hierarchy. – Michael K Mar 19 '12 at 18:57
  • @DeadMG: That's not (even close to) direct support for expressing a hierarchy of abstractions. It's nearly a perfect example of sufficient flexibility to simulate what's needed without supporting it directly. – Jerry Coffin Mar 19 '12 at 19:22
  • @JerryCoffin: The only difference between doing that in C and inheritance in C++ is that in C, you have to make an explicit cast, and in C++, you don't. – DeadMG Mar 19 '12 at 19:50
  • @DeadMG: Actually, there's quite a bit more than that, such as directly expression inheritance vs. embedding. – Jerry Coffin Mar 19 '12 at 20:03
  • Since one should favor composition over inheritance (http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance), encapsulation (and namespace) are probably the biggest concerns. – ChuckCottrill Oct 25 '13 at 21:33
  • This also has a comment about OO being presented as the final model (http://programmers.stackexchange.com/questions/141329/what-makes-c-so-popular-in-the-age-of-oop?rq=1) – ChuckCottrill Oct 25 '13 at 21:34
  • If 90% of the Java production code I see is: non-modular, exposes everything left and right, doesn't have a hierarchy to speak off and abstraction is the thing abstracted away. Does that make Java a non-OO language? – Pieter B Jun 16 '16 at 14:57
  • @PieterB: No, not really. Despite abuse, Java does support the four "pillars" of object orientation. I'm tempted to paraphrase Dijkstra, and say: "Java is a mistake, carried through to perfection. It is the language of the future for the programming techniques of the past: it creates a new generation of coding bums." That's accurate to the extent that it has, indeed, created generations of "coding bums", but wrong in claiming that it has carried anything through to (anything approaching) perfection. – Jerry Coffin Jun 16 '16 at 15:15
5

According to Rumbaugh et al., "Object-Oriented Modelling and Design", objects are characterized by the following features:

  • Identity: data is quantized into discrete, distinguishable entities, called objects.
  • Classification: objects with the same structure and behaviour are grouped together to form classes.
  • Polymorphism: the same operation (an operation with the same name) may behave differently on different classes.
  • Inheritance: sharing of attributes and operations among classes based on a hierarchical relationship.

According to this definition, an OO-language should support these concepts natively. In C you can define struct's which share structure, but not behaviour (structure classification without behaviour classification). You can define identity by means of handles to data objects (pointers). Polymorphism and inheritance are missing altogether and must be implemented through ad-hoc techniques.

So, according to these criteria, C is not an object-oriented language. Using some ad-hoc techniques you can implemented and object-oriented design in C, but C does not have enough concepts to support it directly.

Giorgio
  • 19,486
  • 16
  • 84
  • 135
2

The only realistic requirement for a language to be OOP is that it's possible to expend at least a good proportion of the time actually dealing with OOP instead of hacking around it's deficiencies. Something like e.g. C is hardly great, but it's usable. If you're desperate.

Which programming languages are purest OOP? Which denominations are the purest Christians? There's no way you could possibly get a correct or objective answer to that question- there's not even any useful definition of "pure". And secondly, even if you could pick the language which had no other non-OOP features and was "pure" in the scientific definition, i.e., only of one thing, then it would be a terrible language. OOP is not some wonder-child, it's not good for everything by miles, and only offering OOP sends a language right down the pile of awful.

DeadMG
  • 36,794
  • 8
  • 70
  • 139
  • "The only realistic requirement for a language to be OOP is that it's possible to expend at least a good proportion of the time actually dealing with OOP instead of solving the actual problem." there, fixed for you :) just kidding – Luiz Felipe Mar 19 '20 at 02:34
2

It's hard to tell what it means for a language to be object-oriented. Actually, it's hard to tell what it means for a language to be X, where X can be OO, FP, anything.

The official definition of Object-Orientation is

  • messaging,
  • local retention and protection and hiding of state-process, and
  • extreme late-binding of all things

But note that this describes properties of a program, not a language.

Personally, I would say that a language is object-oriented if it facilitates and encourages building programs that satisfy the above definition. Saying that a language is OO if it allows that, is pointless: every Turing-complete language allows everything. The question is: how easy is it to use?

C certainly allows functional programming. And object oriented programming. And logic programming. But none of these is easy in C, nor does C facilitate or encourage programming in those styles.

Also, note that the above definition doesn't mention anything about classes. Why would it? After all, it's called OOP, not COP. In fact, it doesn't even mention objects! Alan Kay himself says that he regrets calling it object-oriented, and that he should rather have called it message-oriented instead, since the important thing are messages, not objects (and certainly not classes).

Jörg W Mittag
  • 101,921
  • 24
  • 218
  • 318
  • The definition above does not mention objects, but how would you call the senders and receivers of messages? – Giorgio Mar 21 '12 at 19:55
1

Liskov substitution principle is pretty much the defn of Object Orientated.

Although in practice encapsulation and data hiding are probably more useful

Martin Beckett
  • 15,776
  • 3
  • 42
  • 69
  • Its not, if you read Liskov paper you will understand that the substitution principle is an software engineering technique, you can even apply it to function programming. – Luiz Felipe Mar 19 '20 at 02:24
1

Many years ago I read about this topic in a JavaScript book, I cannot remember which one it is. But I can still remember the discussion about whether JavaScript should be considered as an OOP language, the author's answer is yes, coz it meets the four criteria for a language to be object oriented:

  1. Encapsulation, which allows you to combine data and procedures that operates the data together.
  2. Inheritance (pretty simple, someone has explained above)
  3. Polymorphism (someone has explained above)
  4. Composition, which allows you to define an object of a class as another class's member data.
VincentZHANG
  • 171
  • 1
  • 5