tags:

views:

243

answers:

9

which come first class or object in object oriented languages ?

+2  A: 

An object is an instantiation of a class. The class comes first.

The class represents a blueprint or template of an object. It is the exact layout, which describes how to build the object. In the classical OO sense, the class is required first in order to build the object.

It is a philosophical question whether, in real-life, the top-down approach (class first, then object) or bottom-up approach (object first, then class abstraction) is better.

Lastly, apparently someone can come up with a language that employs OO concepts without classes, but IMO that is not really what the question was about.

cschol
... maybe but not in oop languages that have no classes.
cartoonfox
Language can't be object oriented if there isn't anything to create the object from in the first place. Certainly just having objects doesn't make the language OOP, but it's quite fundamental need for one.
Esko
prototype based OO languages and trait based OO languages do not require classes.
Pete Kirkham
@cartoonfox, @Pete Kirkham: maybe, but do you really think that's what the op was after?
cschol
Note that the question is tagged "c++" and "java", not, say, "javascript" and "self".
Pavel Minaev
A: 

Think of a class as a cookie cutter and the object as the cookies. Classes are specifications; objects are realizations of those specifications in memory.

Classes come first. Can't have an object without a class.

duffymo
A: 

I would say its Class. Imagine a a human body template without life and attributes.And when you give the above said blood and heart its beautiful and live. class is just a template and object is one thing with all the properties and an individual.

nobody
A: 

According to wikipedia the first language with explicit feature support for OOP was Simula which included the concept of a class. More recently there have been OOP langugages which use prototypes rather than classes such as javascript.

Steve Homer
Simula does *not* have classes nor inheritance. Classes and Inheritance were added much later, in Simula-67. Smalltalk is inspired by Simula and originally didn't have classes or inheritance, either. Those were added in Smalltalk-74 (or maybe Smalltalk-72), I think. So, *both* the language which introduced the term "object" *and* the language which introduced the term "object-oriented programming" *do not* have classes. And Alan Kay has publicly denounced the way that current "class-oriented programming" is done as anti-OOP.
Jörg W Mittag
Interesting thanks for the correction.
Steve Homer
Simula design papers are an interesting read, and make it clear that language designers really considered the language not quite complete until the introduction of generalizing concept of classes in 67.
Pavel Minaev
+2  A: 

It's a lot easier to come up with a good design if you think in terms of the objects first and then work out what classes you need to make them.

Not all OOP languages have classes - and you don't need them.

In Ruby for example you can define new methods on an object and just clone the objects themselves - or you can do traditional design and define new classes.

In Self you don't define classes at all. Rather you add methods to a "prototypical example object" and clone copies of it when you need new "instances".

Lots of people like to start designing OO systems with class diagrams but unfortunately, that's a symptom of SQL thinking rather than OO thinking. Above all else - objects do things.

cartoonfox
The fact that people when they talk about "UML diagram" automatically assume that to mean "UML Class diagram", even though UML also contains several variants of Object diagrams is truly sad. As you said: classes don't *do* anything, objects do. And that's what programming is all about: making computers *do* things.
Jörg W Mittag
I wouldn't say that classes don't do anything. They can be a good place for some of the work of creating and configuring instances - but not in languages like Java and C++ that don't actually have class methods (e.g. you can't handle statics polymorphically... i.e. they don't play by OO rules)
cartoonfox
Well, but the *reason* why that doesn't work in Java and C++ and *does* work in Ruby and Smalltalk, is because in Ruby and Smalltalk classes are ... *TADA* ... objects and in Java and C++ they aren't. In fact, Ruby and Smalltalk don't have class methods, either. They are just regular instance methods of the class object's metaclass.
Jörg W Mittag
A: 

In both Java and C++, class comes first. There can be no object without a class.

A class is like a blueprint. The object the car. With one blueprints, many cars can be built. With one class, many objects can be instantiated.

This in contrast to prototype-based OOP languages, like JavaScript. There you create a generic object, and turn give it its features separately.

blwy10
A: 

But still in real life we have things like particles and atoms. We described their details millions of years later - they existed fine without any 'class' or definition how to make them.

Class is a blue-print of the object, it is build of base forms like integers, strings etc. So in theory object 'typeof(object)' is the most base form and it does not require any class.

Therefore I would say object was first, unless someone will present me a class definition of object.

Object as 'idea' definitely came first, we created classes to describe properties of each object.

rochal
A: 

A class is like the total ingredients and procedure for making a soup, while an object is the soup it self.

The ingredients can be objects its own right, so the class definition for an ingredient would be for example: The name of the ingredient, the color, the size, and what you can do with the ingredient.

Programming Example:

class You {
   string name;
   string height;
   string complexion;

   void run() {

   }

   void swim() {

   }

   ...
}

I hope this helps.

Colour Blend
A: 

The short answer is that objects come first.

The long answer is well - long :)

  1. These languages are called object oriented, for reason.
  2. Every OO language employees the mantra everything is an object. This includes classes.
  3. There are even languages which although not OO, support OO programming (like Erlang)
  4. We can think of a classes really in different terms:
    • In most class bases based languages the main reason for using classes is supporting Nominative typing. That is the possibility to distinct to objects by their "type" (for instance Fruit and Star).
    • Classes commonly plays naming role. I mean that some OO languages (like Java) does not have explicit notion of environment, it is played by all classes currently in the classpath.
    • A class can be also used as a template for creating new objects, and consequently
    • A class is commonly an optimization hack - not to duplicate information common to all instances of the class (for instance methods)

I'm sure there are a lot more but this is just for starters :)

Nikolay Petrov
"Every OO language employees the mantra everything is an object. This includes classes." - this is simply not true. C++ classes aren't objects, and not everything is an object in either Java or C++. Historically, in Simula-67 (the first OO language, the one that came up with terms "class" and "object" in the first place) didn't have classes-as-objects, or everything-is-an-object.
Pavel Minaev
I've should have said most. And not following this rule actually hurts the language. About C++ I'll quote Alan Kay "Actually I made up the term "object-oriented", and I can tell you I did not have C++ in mind. ". In Java I could picture some reasons for including primitives (like optimization, likeliness to C/C++, no need for operators and operator overloading), but they also hurt the language.
Nikolay Petrov