-1

I'm almost new to programming and I came to this question that: why should object carry code along with data? isn't packing data enough?

For example: Instead of having 5 employee objects that each has a getDataOfBirth() method (consuming more memory), have a single method in global space and have 5 object with only attributes(smaller objects).

Am I getting something wrong? Is my question even considered general and possible to be occurred in every newbie's mind?

  • 2
    You are getting something wrong. Code is not duplicated. All object instances use same code behind the curtains. – Dalija Prasnikar Apr 29 '16 at 08:07
  • 1
    It probably depends on the language, but in all languages I know of, functions are not stored inside objects. – Quentin Apr 29 '16 at 08:07
  • 1
    This question might help [What's the method representation in memory?](http://stackoverflow.com/questions/8204595/whats-the-method-representation-in-memory) – Dalija Prasnikar Apr 29 '16 at 08:11

1 Answers1

2

The linguistic aspect of it:

This is an idea that OOP skeptics have been talking about for a long time, but it's more of a matter of preference I would say. If you are new to programming and already are thinking about these things, then maybe functional programming would make a lot of sense to you.

The memory aspect of it:

The functions are typically not stored inside the objects, so OO objects that have a lot of functions do typically not carry those functions around. This is however an implementation detail but most OOP languages should be thought of like that.

Especially in the case of natively compiled languages like C++, the code and the data will be separated into different memory areas altogether and will not really mix. That is also a bit of an implementation detail but all mainstream operating systems, as far as I know, will allocate memory with code separated from data. The functions of a class will be allocated in one area and the data of the objects in another, and normally all objects of the same class will use the same functions.

dvaergiller
  • 795
  • 3
  • 11
  • What about objects being moved on a wire(Network)? It must carry code along side data? I mean each object. maybe 10000. Is it Right? @dvaergiller – Sir Meysam Ferguson Apr 29 '16 at 08:24
  • 1
    @ZobeyrFa: Generally in most OO languages code cannot be sent over the wire. Therefore most OO languages only serializes data which can be re-created as objects in memory provided the code on the other side have the correct classes described for it. – slebetman Apr 29 '16 at 08:26
  • 1
    That is also an implementation detail. Lets say we are transferring the object to a recipient that has the class definition but not that particular object. In that case the recipient already has the code; it's in the class definition. The code does not have to be sent in that case. There are some implementations though, that can not assume that the recipient has the class definition. Multi agent systems is an example of where that might be useful. In that case, yes code has to be transferred. But I would say the latter case is the less common one. – dvaergiller Apr 29 '16 at 08:29