views:

1295

answers:

12

Since debate without meaningful terms is meaningless, I figured I would point at the elephant in the room and ask: What exactly makes a language "object-oriented"? I'm not looking for a textbook answer here, but one based on your experiences with OO languages that work well in your domain, whatever it may be.

A related question that might help to answer first is: What is the archetype of object-oriented languages and why?

A: 

Supports classes, methods, attributes, encapsulation, data hiding, inheritance, polymorphism, abstraction...?

happyappa
+3  A: 

According to Booch, the following elements: Major:

  • Abstraction
  • Encapsulation
  • Modularity
  • Hierarchy (Inheritance)

Minor:

  • Typing
  • Concurrency
  • Persistence
Vaibhav
+3  A: 

Smalltalk is usually considered the archetypal OO language, although Simula is often cited as the first OO language.

Current OO languages can be loosely categorized by which language they borrow the most concepts from:

  • Smalltalk-like: Ruby, Objective-C
  • Simula-like: C++, Object Pascal, Java, C#
Kristopher Johnson
Don't forget Objective C and Javascript as coming from the Smalltalk branch
Mamut
A: 

Archetype

The ability to express real-world scenarios in code.

foreach(House house in location.Houses)
{
 foreach(Deliverable mail in new Mailbag(new Deliverable[]
              {
              GetLetters(), 
              GetPackages(), 
              GetAdvertisingJunk()
              })
 {
    if(mail.AddressedTo(house))
    {
        house.Deliver(mail);
    }
 }
}

-

foreach(Deliverable myMail in GetMail())
{
    IReadable readable = myMail as IReadable;
    if ( readable != null )
    {
        Console.WriteLine(readable.Text);
    }
}

Why?

To help us understand this shit more easily. It makes better sense in our heads and if implemented correctly makes the code more efficient, re-usable and reduces repetition.

To achieve this you need:

  • Pointers/References to ensure that this == this and this != that.
  • Classes to point to (e.g. Arm) that store data (int hairyness) and operations (Throw(IThrowable))
  • Polymorphism (Inheritance and/or Interfaces) to treat specific objects in a generic fashion so you can read books as well as graffiti on the wall (both implement IReadable)
  • Encapsulation because an apple doesn't expose an Atoms[] property
Quibblesome
+12  A: 

Definitions for Object-Orientation are of course a huge can of worms, but here are my 2 cents:

To me, Object-Orientation is all about objects that collaborate by sending messages. That is, to me, the single most important trait of an object-oriented language.

If I had to put up an ordered list of all the features that an object-oriented language must have, it would look like this:

  1. Objects sending messages to other objects
  2. Everything is an Object
  3. Late Binding
  4. Subtype Polymorphism
  5. Inheritance or something similarly expressive, like Delegation
  6. Encapsulation
  7. Information Hiding
  8. Abstraction

Obviously, this list is very controversial, since it excludes a great variety of languages that are widely regarded as object-oriented, such as Java, C# and C++, all of which violate points 1, 2 and 3. However, there is no doubt that those languages allow for object-oriented programming (but so does C) and even facilitate it (which C doesn't). So, I have come to call languages that satisfy those requirements "purely object-oriented".

As archetypical object-oriented languages I would name Self and Newspeak.

Both satisfy the above-mentioned requirements. Both are inspired by and successors to Smalltalk, and both actually manage to be "more OO" in some sense. The things that I like about Self and Newspeak are that both take the message sending paradigm to the extreme (Newspeak even more so than Self).

In Newspeak, everything is a message send. There are no instance variables, no fields, no attributes, no constants, no class names. They are all emulated by using getters and setters.

In Self, there are no classes, only objects. This emphasizes, what OO is really about: objects, not classes.

Jörg W Mittag
Messages, late binding, and polymorphism are the same thing: an early-bound message is just a procedure call, and if you can't send the same messages to objects of different kinds, then why late bind?Delegation, encapsulation, and everything is an object also derive from message passing, in a way.
Damien Pollet
+1  A: 

As far as I can tell, the main view of what makes a language "Object Oriented" is supporting the idea of grouping data, and methods that work on that data, which is generally achieved through classes, modules, inheritance, polymorphism, etc.

See this discussion for an overview of what people think (thought?) Object-Orientation means.

As for the "archetypal" OO language - that is indeed Smalltalk, as Kristopher pointed out.

jeremiahd
+2  A: 

It's not really the languages that are OO, it's the code.

It is possible to write object-oriented C code (with structs and even function pointer members, if you wish) and I have seen some pretty good examples of it. (Quake 2/3 SDK comes to mind.) It is also definitely possible to write procedural (i.e. non-OO) code in C++.

Given that, I'd say it's the language's support for writing good OO code that makes it an "Object Oriented Language." I would never bother with using function pointer members in structs in C, for example, for what would be ordinary member functions; therefore I will say that C is not an OO language.

(Expanding on this, one could say that Python is not object oriented, either, with the mandatory "self" reference on every step and constructors called init, whatnot; but that's a Religious Discussion.)

aib
just to take the bait for a second, why on earth would not having an implicit self keyword mean that python isn't OO?
interstar
I'm sorry, what?
aib
A: 

Disregarding the theoretical implications, it seems to be

"Any language that has a keyword called 'class'" :-P

Orion Edwards
funny ... but wrong
interstar
A: 

To further what aib said, I would say that a language isn't really object oriented unless the standard libraries that are available are object oriented. The biggest example of this is PHP. Although it supports all the standard object oriented concepts, the fact that such a large percentage of the standard libraries aren't object oriented means that it's almost impossible to write your code in an object oriented way.

It doesn't matter that they are introducing namespaces if all the standard libraries still require you to prefix all your function calls with stuff like mysql_ and pgsql_, when in a language that supported namespaces in the actual API, you could get rid of functions with mysql_ and have just a simple "include system.db.mysql.*" at the top of your file so that it would know where those things came from.

Kibbee
+2  A: 

Basically Object Oriented really boils down to "message passing"

In a procedural language, I call a function like this :

  f(x)

And the name f is probably bound to a particular block of code at compile time. (Unless this is a procedural language with higher order functions or pointers to functions, but lets ignore that possibility for a second.) So this line of code can only mean one unambiguous thing.

In an object oriented language I pass a message to an object, perhaps like this :

 o.m(x)

In this case. m is not the name of a block of code, but a "method selector" and which block of code gets called actually depends on the object o in some way. This line of code is more ambiguous or general because it can mean different things in different situations, depending on o.

In the majority of OO languages, the object o has a "class", and the class determines which block of code is called. In a couple of OO languages (most famously, Javascript) o doesn't have a class, but has methods directly attached to it at runtime, or has inherited them from a prototype.

My demarcation is that neither classes nor inheritance are necessary for a language to be OO. But this polymorphic handling of messages is essential.

Although you can fake this with function pointers in say C, that's not sufficient for C to be called an OO language, because you're going to have to implement your own infrastructure. You can do that, and a OO style is possible, but the language hasn't given it to you.

interstar
A: 

when you can make classes, it is object-oriented
for example : java is object-oriented, javascript is not, and c++ looks like some kind of "object-curious" language

A: 

In my experience, languages are not object-oriented, code is.

A few years ago I was writing a suite of programs in AppleScript, which doesn't really enforce any object-oriented features, when I started to grok OO. It's clumsy to write Objects in AppleScript, although it is possible to create classes, constructors, and so forth if you take the time to figure out how.

The language was the correct language for the domain: getting different programs on the Macintosh to work together to accomplish some automatic tasks based on input files. Taking the trouble to self-enforce an object-oriented style was the correct programming choice because it resulted in code that was easier to trouble-shoot, test, and understand.

The feature that I noticed the most in changing that code over from procedural to OO was encapsulation: both of properties and method calls.

philosodad